473,394 Members | 1,706 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,394 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 17505
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.