3

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 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

I can't seem to replicate the problem on my development machine, only on the server.
Therefore I would implement the logging into both. If the error happens a second time you now have the detailed data which caused the error. In most cases it is not easy to replicate a error cause you do not exactly know what the user has all done to your front end to produce this. And for sure there is always a DAU

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.