Updated data is stored truncated (exactly one character long) in DB

Updated data is stored truncated (exactly one character long) in DB

Old forum URL: forums.lhotka.net/forums/t/1373.aspx


SQueek posted on Tuesday, October 03, 2006

Maybe another noob error  but I don't get the solution. When I send an update query to the database, the values in the updated table only shows the first character of the value passed to the query. The business object contains the correct values when passed to the StoredProcedure.

Would appreciate any help for getting this error done.

Thank you

Mario

figuerres replied on Tuesday, October 03, 2006

Are you using CodeSmith to create your stored procs ??

there is a bug in the class they supply that processes nvarchar(max) or varchar(max) as 1 char.

or see if anywhere else this might be happening.

I had this happen to me and I found the problem and reported it to CodeSmith.

my quick fix was to copy GetSqlParameterStatement() into my codebehind and add a tiny bit of code:

    if (column.NativeType != "text" && column.NativeType != "ntext"){
     if (column.Size > 0)
      param += "(" + column.Size + ")";
     else
      if( column.Size == -1)
       param += "(MAX)";
    }

this chnaged the proc from:

foo nvarchar,

to

foo nvarchar(MAX),

which in my case fixed the bug.

SQueek replied on Tuesday, October 03, 2006

Thanks for your reply. I don't use CodeSmith templates but your answer lead to the solution. But to be true, I hadn't understood why it worked ;-) Now I do. Varchar size is defined as bytes and nvarchar datatypes size are defined as characters.

I changed varchars to nvarchar. After that updates worked like exptected.

Greetings Mario

Copyright (c) Marimer LLC