Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

try
{
//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",
(object)txtImagePath.Text));

cmd.Parameters.Add(new SqlParameter("@i1",
(object)imageSampleData));

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


Please help me.
Posted
Updated 2-Jun-10 21:45pm
v2

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]
 
Share this answer
 
v2
Comments
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)



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