By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

Insert images to MySQL with C#

P: n/a
Hi all,

I am using OleDbConnection for connecting with MySQL.Using INSERT query for inserting images to MySQL db with C# code.
The code is given below :

string strConn="PROVIDER=MySQLProv;SERVER=192.168.1.8;DB= test;UID=test;PWD=;PORT=;";
OleDbConnection objConn;
objConn=new OleDbConnection (strConn);
objConn.Open();
Image image1=pictureBox.Image;
image1.Save ( textBox1.Text ,ImageFormat.Bmp );
System.IO .FileStream stream =new FileStream ("d://shri.bmp" ,System.IO.FileMode .Open ,System.IO.FileAccess .Read );
System.Int32 flength=0;
flength=(int)stream.Length;
System.Byte[] buffer=new Byte[flength ];
string imagename=textBox1.Text+count ;
string strname="shri";
string sql="INSERT INTO tblcustomer VALUES(?,?,?)";
OleDbCommand cmd = new OleDbCommand(sql,objConn);
cmd.Parameters .Add (new OleDbParameter ("@varName",strname));
cmd.Parameters .Add (new OleDbParameter ("@varImage",imagename));
cmd.Parameters .Add (new OleDbParameter ("@image",OleDbType.Binary ).Value =buffer);
cmd.ExecuteNonQuery();
objConn.Close ();

The Error is:

An unhandled exception of type 'System.InvalidCastException' occurred in system.data.dll

Additional information: The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not Byte[] objects.
Waiting for Reply
-Shri

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi shri,

Try to this:
OleDbParameter param = new OleDbParameter ("@image",OleDbType.Binary );
param.Value =buffer;
cmd.Parameters .Add (param);

instead of
cmd.Parameters .Add (new OleDbParameter ("@image",OleDbType.Binary ).Value
=buffer);

You weren't passing an instance of OleDbParameter...

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
"shri" <sh*********@yahoo.com> wrote in message
news:Ok**************@TK2MSFTNGP09.phx.gbl...
Hi all,

I am using OleDbConnection for connecting with MySQL.Using INSERT query for inserting images to MySQL db with C# code. The code is given below :

string strConn="PROVIDER=MySQLProv;SERVER=192.168.1.8;DB= test;UID=test;PWD=;PORT=;"
; OleDbConnection objConn;
objConn=new OleDbConnection (strConn);
objConn.Open();
Image image1=pictureBox.Image;
image1.Save ( textBox1.Text ,ImageFormat.Bmp );
System.IO .FileStream stream =new FileStream ("d://shri.bmp" ,System.IO.FileMode .Open ,System.IO.FileAccess .Read ); System.Int32 flength=0;
flength=(int)stream.Length;
System.Byte[] buffer=new Byte[flength ];
string imagename=textBox1.Text+count ; string strname="shri"; string sql="INSERT INTO tblcustomer VALUES(?,?,?)";
OleDbCommand cmd = new OleDbCommand(sql,objConn);
cmd.Parameters .Add (new OleDbParameter ("@varName",strname));
cmd.Parameters .Add (new OleDbParameter ("@varImage",imagename));
cmd.Parameters .Add (new OleDbParameter "@image",OleDbType.Binary ).Value =buffer); cmd.ExecuteNonQuery();
objConn.Close ();

The Error is:

An unhandled exception of type 'System.InvalidCastException' occurred in system.data.dll
Additional information: The OleDbParameterCollection only accepts non-null OleDbParameter type objects, not Byte[] objects.

Waiting for Reply
-Shri

************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP &

ASP.NET resources...
Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.