Click here to Skip to main content
11,496,146 members (1,595 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C# MySQL
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 22:28pm
Edited 2-Jun-10 22:45pm
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 | :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 at 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
0 Dnyaneshwar@Pune 692
1 RyanDev 230
2 CHill60 208
3 Sascha Lefèvre 195
4 Sergey Alexandrovich Kryukov 124
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 15 Jun 2011
Copyright © CodeProject, 1999-2015
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