Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
 
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 2-Jun-10 22:28pm
Edited 2-Jun-10 22:45pm
v2
Rate this: bad
good
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 | :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]
  Permalink  
v2
Comments
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 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 Richard MacCutchan 145
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,712
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


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