Troubleshooting String or binary data would be truncated error.

Status
Not open for further replies.
I have a C# Winforms application that is inserting data into a SQL 2005 server using LINQ.

A very intermittent error (probably happens about 1 time per 1,000 user form submissions) is occurring: String or binary data would be truncated.

When this error occurs, the submission is not inserted in the database. My understanding is this error occurs when you attempt to insert a value into a field that is too narrow to contain the data being inserted.

My question is, does anyone know of a tool or way to check the MS SQL logs to see what field this is happening to? The database is relatively low traffic, so I don't mind manually looking through a log (if I knew what I was looking for). There is no DB admin to assist on the project, and my goal is to restrict user input in the software to prevent the error from happening, but so far I have been unable to find the field causing the problem.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
This may not be _a_ field as such IIRC this can also happen if you sum all the fields (including the actual lengths of any varchars) and it is greater than ~8K.

Could be wrong, It's a while since I had to deal with that.

As to find out why I suggest a simple try { /* inster */ } catch (SqlException) { /* dump the attempted insert values */; throw } will get you a complete list.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
ShuggyCoUk":jhbsfx36 said:
This may not be _a_ field as such IIRC this can also happen if you sum all the fields (including the actual lengths of any varchars) and it is greater than ~8K.

Could be wrong, It's a while since I had to deal with that.

As to find out why I suggest a simple try { /* inster */ } catch (SqlException) { /* dump the attempted insert values */; throw } will get you a complete list.

I've seen similar intermittent errors like that with other databases as well. Same cause; too many characters in total, not a specific field. (And noticed a comment in one database saying somehting like "Oh, if you get this error, call this function instead..." like their own library couldn't have done that for you...
 
Status
Not open for further replies.