Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get "String or binary data would be truncated" as an error?

I have a problem with this:

String or binary data would be truncated

And this is my coding:

String savePath = @"C:\Users\Shen\Desktop\LenzOCR\LenzOCR\WindowsFormsApplication1\ImageFile\" + fileName;

inputImageBox.Image = Image.FromFile(ImageLocation);
inputImageBox.Image.Save(savePath);

String sqlData = "INSERT INTO CharacterOCR(ImageName, ImagePath, Character, CharacterDescription)    
            VALUES('"+fileName+"', '"+savePath+"', '"+typeName+"', '"+CharDesc+"')";
SqlCommand cmd = new SqlCommand(sqlData, con);
cmd.ExecuteNonQuery(); // <<<<<error indicates here

What is the problem?

like image 504
joonshen Avatar asked May 03 '26 16:05

joonshen


2 Answers

Look at the schema of your table and look at the max lengths. You're probably trying to insert something larger than the max length.

like image 110
Roly Avatar answered May 05 '26 04:05

Roly


That just means that one of the values you're trying to cram into a column is too long for the data type you've defined for that column.

For example, trying to INSERT "Hello" to a column that's only a VARCHAR(4).

like image 26
Richard Neil Ilagan Avatar answered May 05 '26 05:05

Richard Neil Ilagan