Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET MySQL
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 16-Feb-11 0:16am
Comments
AlbinAbel at 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
Ramalinga Koushik at 16-Feb-11 6:48am
   
What coding you have done? Or you want us to do your Homework?
shanthikalai at 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 at 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 at 16-Feb-11 10:22am
   
why don't you try the example I given? save the blob (encoded string)
Sandeep Mewara at 16-Feb-11 12:26pm
   
Please dont ask for 'full code'
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi
 
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);
 
        //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
  Permalink  
Comments
Member 9503303 at 16-Jul-13 1:49am
   
Its very usefull thank u..
Adarsh N at 8-Jun-14 23:39pm
   
Perfect solution.... Short and simple.... was just brilliant
Rate this: bad
good
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 www.rentacoder.com[^]
  Permalink  
v2
Comments
shanthikalai at 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
good
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.
  Permalink  
Rate this: bad
good
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(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...
  Permalink  
Rate this: bad
good
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)
       {
           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();
 
          
       }
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"]);              
           }
       }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,034
1 Sergey Alexandrovich Kryukov 4,838
2 Peter Leow 3,129
3 Maciej Los 3,079
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2014
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