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 error occurs when C# (model) tries to store data records for a column in a SQL SERVER database table that has a smaller size specified but a longer value to provide to this column.

You simply need to use a SQL Server script to change a table's field to resolve this problem.

The only solution is to make the table's columns larger. The programme does not need to be re-deployed on PROD/TEST environments.

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

Popular Answer

Refer to the example below.

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

INSERT INTO MyTable VALUES (1, 'test')

Look at column 1; it has a size of 3, but the value is supplied with a length of 4, therefore you would encounter an error.

Correcting the mistake

The string value you should supply must be less than or equal to its length, in this case, 3 characters, as seen below.

INSERT INTO MyTable VALUES (1, 'tes')

If you wish to stop this mistake

You may disable the ansi warnings option by setting it to off.

SET ansi_warnings OFF

The error would be silenced and whatever can fit in the column would be entered if we set ansi warnings to OFF. The remaining data would be truncated.

INSERT INTO MyTable VALUES (1, 'test') 

There won't be any errors returned since the string "tes" will be kept in your table.



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