3
votes

I have a table and am using ASP.NET MVC to create an object and save it. It's been working fine for a while. Somehow, recently, I started getting this error message on the production machine:

System.Data.SqlClient.SqlException: String or binary data would be truncated.

I can't seem to replicate the same error on my development machine. Is there anyway to find out what column is causing this? I'm running MSSQL 2008 R2 Express.

2

2 Answers

5
votes

This usually occurs when you're trying to insert data that is longer than the field size defined on SQL Server. You can increase the size of the field in the database to solve the problem. If you're accepting user input and saving it to the database then you should be validating it against the defined maximum size of the database field.

1
votes

Caused by data-length mismatch between your insert data and the defined column in your database. It indicates that the database would have to cut of data from your insert to be able to store it.

You may take closer look to the inner exception, I don't know it for sure, but I think you find more information there about the column that throws that error. And yes, you should prevent the users from inserting too long values and catch this in your model.

If you, for what ever don't want to implement a additional validation you should log the exception with the content that you tried to insert/update and then compare the values with the db.