11

Is there a clever way to determine which field is causing 'string or binary data would be truncated' with LINQ.

I've always ended up doing it manually by stepping through a debugger, but with a batch using 'SubmitChanges' I have to change my code to inserting a single row to find the culprit in a batch of rows.

Am I missing something or in this day and age do I really have to still use a brute force method to find the problem.

Please dont give me advice on avoiding this error in future (unless its something much cleverer than 'validate your data'). The source data is coming from a different system where I dont have full control anyway - plus I want to be lazy.

PS. Does SQL Server 2008 actually tell me the field name. Please tell me it does! I'll upgrade!

3
  • just for fun: turned out to be a company name of 'Embassy of the Dominican Republic in the United States' trying to fit into a 50 character company name field Nov 20, 2008 at 19:15
  • Looks like this doesn't work for the entity framework. Hope they get that fixed soon. Jun 21, 2010 at 5:12
  • Recently noticed the field name AND bad data is now included in the error message. Not sure exactly when they added it. We're using SQL Azure with the latest compatibility level. Feb 23, 2020 at 19:47

4 Answers 4

5

Fire up SQL Profiler. The last query from your application should be the one that is failing.

1

Seems like they fixed this problem for the Service Pack 1 of .NET 3.5, as noted in this link: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=353232

Might want to check it out yourself.

2
  • It is not fixed in my version of .net 3.5SP1 - or at least I do not understand how to get to this information.
    – Erwin
    Oct 29, 2009 at 15:06
  • 2
    they haven't fixed it in 4.0 either
    – Mario
    Feb 18, 2011 at 23:01
0

It would probably be useful to re-post/edit this tagged as linq .net

DJ - where are you getting this data you post as a proposed solution ?

The exception doesn't contain this information, so your post is pretty much useless - unless you can specify the source.

-2

Assuming you are processing a large batch of records and you don't want to (can't) validate the data before hand and you just want db constraints to catch any errors?

Change your program to catch the exception and print out the current record details to a log file or something.

Record # 9999
Caused "string or binary data would be truncated" error
Field1: "Data" Length: 55
Field2: 9999
etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.