Click here to Skip to main content
13,046,184 members (86,459 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi everyone..

Need help to upload picture from C# windows application. Unlucky me.. ive got error --> string or binary data would be truncated.the statement has been terminated

//Read Image Bytes into a byte array
byte[] imageSampleData = ReadImageToBytes(txtImagePath.Text);

//Initialize SQL Server Connection
SqlConnection con = new SqlConnection(txtConnectionString.Text);

//Set insert query
string query = "INSERT INTO p (ee,i1) values(@ee,@i1)";

//Initialize SqlCommand object for insert.
SqlCommand cmd = new SqlCommand(query, con);

cmd.Parameters.Add(new SqlParameter("@ee",

cmd.Parameters.Add(new SqlParameter("@i1",

//Open connection and execute insert query.
cmd.ExecuteNonQuery();---> error occur here!

Please help me.
Posted 2-Jun-10 21:28pm
Updated 2-Jun-10 21:45pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Change your database definition!
Look at making the "i1" field a BLOB, MEDIUMBLOB or LONGBLOB depending on the size of the image data.

Plus, do us all a favour: USE SENSIBLE NAMES!

"p" is not a good table name.
"ee" is not a good field, nor is "i1".

What do these names say about the data they contain? Nothing.
What do these names say about the database designer? That he is an inexperienced idiot... :laugh:

[edit]In addition, have a look at the mySQLCommand.AddWithValue method - it will make your code a lot cleaner. Well done, BTW, for using parametrised queries - most beginners miss that. OriginalGriff[/edit]
MissNano 3-Jun-10 5:13am
the ee (nvarchar) and i1 (image)..

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 15 Jun 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100