Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.

Source Error:
C#
 63  con.Close();
 64  con.Open();
 65  int result = (Int32)cmd2.ExecuteScalar();
 66  con.Close();
Posted
Updated 2-Mar-13 3:59am
v5
Comments
karthikeyan9966 2-Mar-13 9:43am    
i am using sql2008..and i frequently get this error on executescalar cmd..can u pls help me out..

1 solution

All it means is that you are trying to insert (or update) data to a field that has a fixed length, and the data you are passing does not fit.
For example, if your table has a UserName column that is nChar(10), and you insert 'MikeyJones' then it will work, but 'MikeyXJones' will cause a "String or binary data would be truncated." error as eleven character will not fit.

Either check and truncate before you modify the database data, or enlarge the column data size.
 
Share this answer
 
Comments
karthikeyan9966 2-Mar-13 10:35am    
yes i have used varchar(max) but still i get this error..
OriginalGriff 2-Mar-13 10:37am    
What columns are you inserting? What are the column definitions?
karthikeyan9966 2-Mar-13 10:55am    
for file upload i've used varchar(max) data type..
OriginalGriff 2-Mar-13 11:12am    
Ah. And what kind of file are you expecting...
In all probability you shouldn't be using varchar - you should most likely be using varbinary(max) or image (possibly text or ntext) - varchar and varbinary are both limited to 8000 bytes where the others support 2Gbytes.
karthikeyan9966 2-Mar-13 11:32am    
ok now i have reduced the size of upload file..but to redirect to next page i use executescalar command but i get error on that particular line..as unhandled sqlexception executescalar command..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900