473,322 Members | 1,781 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,322 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 17499
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:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.