Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
i want to upload an image file and save that into MySql database...and retrive those saved images in asp.net page..please give me full coding,using MySql,C#,ASP.net....
Posted
Comments
Albin Abel 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 http://stackoverflow.com/questions/46788/how-to-bind-a-memorystream-to-aspimage-control
[no name] 16-Feb-11 6:48am    
What coding you have done? Or you want us to do your Homework?
shanthikalai 16-Feb-11 7:06am    
con.Open();
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;
cmd.Parameters.Add(pr);
cmd.ExecuteNonQuery();

Label1.Text = "ad";


this is my coding pls reviw and chk whether it is rit....it takes null values into Mysql ...so 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....
Albin Abel 16-Feb-11 10:22am    
why don't you try the example I given? save the blob (encoded string)

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

C#
protected void btnsave2_Click(object sender, EventArgs e)
       {
           imag++;
       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());
           cmd.ExecuteNonQuery();

          
       }

C#
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())
           {
               Response.BinaryWrite((byte[])dr["Image"]);              
           }
       }
 
Share this answer
 
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(pm);
//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...
 
Share this answer
 
Hope this[^] link might give you an idea.

We are here to help you only. Not to do your Homework.
 
Share this answer
 
Hi
You can get the image data from the file uploads posted file property. In the submit button click event...
C#
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);

    //STORE THIS STRING IN THE BLOB COLUMN IN MYSQL
    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
 
Share this answer
 
Comments
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
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 www.rentacoder.com[^]
 
Share this answer
 
v2
Comments
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..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900