473,320 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Insert images to MySQL with C#

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
1 17498
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jn | last post by:
I have a page that shows info from MySQL. It is for an online education site. It uses different classes arranged in a hierarchy. Each lesson is roughly laid out like this: Lesson |-Intro |...
0
by: Jonas Meurer | last post by:
hello, i develop a project with a mysql interface. one mysql table holds all the images for my project. everything works quite well so far, except i'm not able to upload images into the...
0
by: Peter | last post by:
Hi, I need some advice. I've a program running on a local machine which automatically pushes images from a camera to our webserver (linux/mysql) through FTP. I want to store those images in the...
0
by: David Bordas | last post by:
Hi list, I've got a little bug with MySQL. I can insert a row into my table but this row will not appear in the table :( Server is under linux redhat, MySQL is 3.23.56 installed from binary...
0
by: Lars Rasmussen | last post by:
I tried that, but i dont work either. I need to insert a way that mysql doese'nt complain when i copy some records that have the same id (or that it just gives it an id according to the...
3
by: Frank Natoli | last post by:
Having created a table with a longblob column, what is the insert syntax to set the value of the longblob column? The data will be read from a file.
1
by: shridhar_km | last post by:
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...
0
by: pankajag | last post by:
hi how we can insert any images in mysql database using visual basic 6.0 coding. tanx
2
josie23
by: josie23 | last post by:
Egad, I'm not a coder/programmer by nature or occupation but understand things like html and css and a small amount of perl. So, basically, I'm a perl/mysql imbecile. But, I've been trying to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.