Click here to Skip to main content
13,090,299 members (87,522 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i want to upload an image file and save that into MySql database...and retrive those saved images in page..please give me full coding,using MySql,C#,
Posted 16-Feb-11 0:16am
AlbinAbel 16-Feb-11 6:33am
Hi I have answered your question 2 times. Have you tried that? What you mean full coding? connecting a mysql, creating table, save the data etc.,???!!!!

As you asked this link may give you information @ display an image from memorystream
Ramalinga Koushik 16-Feb-11 6:48am
What coding you have done? Or you want us to do your Homework?
shanthikalai 16-Feb-11 7:06am
FileUpload img = (FileUpload)FileUpload1;
byte[] imgbyte = null;
if (img.HasFile && img.PostedFile != null)

HttpPostedFile File = FileUpload1.PostedFile;
imgbyte = new byte[File.ContentLength];
File.InputStream.Read(imgbyte, 0,(File.ContentLength));
MySqlCommand cmd = new MySqlCommand("INSERT INTO test21(img)VALUES(@img)", con);
MySqlParameter pr = new MySqlParameter("@img",MySqlDbType.VarBinary);
pr.Value = imgbyte;

Label1.Text = "ad";

this is my coding pls reviw and chk whether it is takes null values into Mysql pls help me how to insert an image file....
kavin609 6-Jul-11 6:55am
hello kalai,
this is because my sql accepts only '?' instead of '@' so try by replacing '?' and it will executes.....

i like coding....
AlbinAbel 16-Feb-11 10:22am
why don't you try the example I given? save the blob (encoded string)
Sandeep Mewara 16-Feb-11 12:26pm
Please dont ask for 'full code'
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


You can get the image data from the file uploads posted file property. In the submit button click event...
    protected void Button1_Click(object sender, EventArgs e)
        HttpPostedFile postedFile=FileUpload1.PostedFile;
        Stream stream = postedFile.InputStream;
        BinaryReader bReader=new BinaryReader(stream);
        byte[] bytes=bReader.ReadBytes((int)stream.Length);
        string blob=Convert.ToBase64String(bytes);
        // For testing--- to show the image back to browser
        byte[] bytes1 = Convert.FromBase64String(blob);
        Response.ContentType = postedFile.ContentType;
        Response.OutputStream.Write(bytes1, 0, bytes1.Count());

as show in the code save the blob string to a mySql table column with BLOB data type. Retrive it as shown in the code, you can get a byte stream (memory stream if you want, and also possible to convert it in to bitmap). you may need to use the stream to display
Member 9503303 16-Jul-13 1:49am
Its very usefull thank u..
Adarsh N 8-Jun-14 23:39pm
Perfect solution.... Short and simple.... was just brilliant
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I usually put these type suggestions in Comments but for your question this is an ANSWER.
What have you tried so far? If you would have looked into CodeProject itself, there would be 100s of smiliar threads with proper explained answers.
Now comes the part which irritates me most i.e.: 'Give me full coding'. Why the hell in the world do you think that some one else should do your work? And if you want FULL BAKED CODE, the place is called[^]
shanthikalai 16-Feb-11 7:16am
i'm a beginner tat's y i'm asking lik tat...pls don't hurt me...i'm trying several codings but value of that image tak null values in mysql..
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Hope this[^] link might give you an idea.

We are here to help you only. Not to do your Homework.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

byte[] buffer;
MySqlConnection connection = new MySqlConnection("Database=onlinebilling;Data Source=localhost;User Id=root;Password=root");
string s = FileUpload1.FileName;
HttpPostedFile file = null;
//FileUpload1.SaveAs(Server.MapPath("~/images/") + s);

if(FileUpload1.PostedFile!=null && FileUpload1.PostedFile.FileName!=null)

cmd = new MySqlCommand("INSERT INTO customerdetails(name,contactno,address,photo,date,branch) values('fghdfhgdf','dfhgdfgh','sdfsfsf',?photo,'dsfsfsdf','gsggsgd')", con);
BinaryReader br;

int ig = 0;
file = FileUpload1.PostedFile;
ig = file.ContentLength;
byte[] byt = new byte[ig];
file.InputStream.Read(byt, 0, ig);
//MySqlParameter pm = new MySqlParameter("?photo", MySqlDbType.Blob);
//pm.Size = buffer.Length;
//pm.Value = buffer;
//cmd.Parameters.Add("@photo", MySqlDbType.LongBlob).Value = buffer;
cmd.Parameters.AddWithValue("?photo", byt);

using (connection)
int i = cmd.ExecuteNonQuery();
Label1.Visible = true;
Label1.Text = "uploaded, " + i.ToString() + "Successfully";


it will hepls you sure.....

I like coding...
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

By using this code you can save the image file as binary.
In database create two fields
1.Image varbinary
2.ImageId varchar

protected void btnsave2_Click(object sender, EventArgs e)
       byte[] imagebyte = new byte[FileUpload1.PostedFile.InputStream.Length + 1];
           FileUpload1.PostedFile.InputStream.Read(imagebyte, 0, imagebyte.Length);
           cmd = new MySqlCommand("Insert Into db_image (Image,ImageId) values (@imag,@imageid)", con);
           cmd.Parameters.AddWithValue("imag", imagebyte);
           cmd.Parameters.AddWithValue("imageid", imag.ToString());

protected void btnretr2_Click(object sender, EventArgs e)
           cmd = new MySqlCommand("Select Image from db_image where ImageId='" +txt_imageid.Text.Trim()+ "' ", con);
           dr = cmd.ExecuteReader();
           while (dr.Read())

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2017
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