String or binary data would be truncated. The statement has been terminated. System.Data.SqlClient.SqlException (0x80131904)

asp.net-mvc entity-framework sql-server

Question

String or binary data would be truncated. The statement has been terminated.

System.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated

1
1
12/14/2016 7:11:16 AM

Accepted Answer

This exception throws when C#(model) try to save data record for column whose size defined less in SQL SERVER database table where value to pass to this column string length in greater.

To fix this error you only need to alter column of table in SQL SERVER database using SQL Server script.

Only increasing size of column in table works. No need to re deploy the application on PROD/TEST environment.

3
7/3/2016 6:43:34 PM

Popular Answer

Please refer this sample below.

CREATE TABLE MyTable(Num INT, Column1 VARCHAR(3))

INSERT INTO MyTable VALUES (1, 'test')

Look at column1 its size is 3 but the given value is of length 4 so you would get the error.

To fix the error:

You should pass the string value less than or equal to it size ie., 3 characters like the below.

INSERT INTO MyTable VALUES (1, 'tes')

If you want to suppress this error

you can use set the below ansi_warnings parameter to off

SET ansi_warnings OFF

if we use ansi_warnings as OFF, the error would be suppressed and whatever can fit in the column, would be inserted, the rest would be truncated.

INSERT INTO MyTable VALUES (1, 'test') 

The string 'tes' would be stored in your table and it won't return any error.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow