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

10,232

Solution 1

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.

Solution 2

It's generally considered better coding practice to store the paths to where your images are in your database, while keeping your images stashed in the appropriate directory structure.

If you're set on inserting image data into a database, kindly let everyone know what version your database is.

EDIT:

Pull up your SQL Schema for the ImagePath column, and tell me how many characters you have it defined to accept.

Next, before you execute your SQL, call Console.WriteLine(savePath.Length).

Is savePath.Length greater than your ImagePath column definition?

Solution 3

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).

Share:
10,232
joonshen
Author by

joonshen

Updated on June 18, 2022

Comments

  • joonshen
    joonshen almost 2 years

    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?