[C#, MS SQL Server] String or binary data would be truncated. But not field length problem.

  • Thread starter Thread starter dtarczynski
  • Start date Start date
Well, I don't know about the message, but encoding / collation could be
an issue... have you tried ntext, nvarchar, etc?

Marc
 
Well, I don't know about the message, but encoding / collation could be
an issue... have you tried ntext, nvarchar, etc?

Marc

Yes, I'd tried ntext, nvarchar, text etc. This doesn't solve my
problem. Any ideas what colation should i chose?
 
In that case I suspect that you are somewhere using default lengths...

specifically, are you specifying the length property of the parameter
when adding it to the collection? In your SQL are you declaring the
parameter with size? Are you doing anything that might mess this up?
Any temp variables? concatenations? trims? Triggers that might be
expecting shorter strings.

I suspect the devil is in the detail here...

Marc
 
In that case I suspect that you are somewhere using default lengths...

specifically, are you specifying the length property of the parameter
when adding it to the collection? In your SQL are you declaring the
parameter with size? Are you doing anything that might mess this up?
Any temp variables? concatenations? trims? Triggers that might be
expecting shorter strings.

I suspect the devil is in the detail here...

Marc

Im exectuting commands as strings for example:
string varStrLongPhrase = "somelongAndProblematicsValues";
cmd.CommandText = String.Format("INSERT INTO table1 VALUES ('{0}');
",varStrLongPhrase);
cmd.ExecuteNonQuery();

And this isnt'working as MS SQL sql scritpt to (when I trying exectue
this in MS Sql server manager.
 
And this isnt'working as MS SQL sql scritpt to (when I trying exectue
this in MS Sql server manager.

Ofcourse in sql manager im trying exectute tsql statemment (Insert into
.....) - not this code above :-)
 
Well, without reproducable code it is hard to say for sure... however,
one first thing to try is parameterised queries, i.e. using the
parameters collection. A stored procedure is optional - you can use
parameters without them. This will also also prevent injection
attacks, but can be especially useful with long strings (esp. text) as
parameters. It would certainly be the next thing I would try...

Marc
 
Back
Top