Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends,

please help me out,

how to insert an image in database through c#(upload a file) and also while retriving it should display in picturebox.

thanking you.
Posted
Comments
An@nd Rajan10 20-Dec-13 5:01am    
stored as binary...

Try this link, works fine.

http://forums.asp.net/t/1922364.aspx[^]

If you wanna store the image file in web server and save the image path in Db you can prefer this link
http://www.aspsnippets.com/Articles/Retrieve-images-using-a-file-path-stored-in-database-in-ASPNet.aspx[^]


Its better to store the images in web server instead of SQL server and save the file path in table column, if you are handling more number of images,
in case of less number of images you can save the image object in the SQL Server.
 
Share this answer
 
Comments
Vishal Pand3y 20-Dec-13 5:08am    
it will be much better to store path instead of image take a 5 for that
Karthik_Mahalingam 20-Dec-13 5:11am    
Thanks vishal :)
Aravindba 20-Dec-13 5:11am    
this is bad idea in web application store a path in database,some time images deleted form folder ,then what u do,it will point out path,but there is no image ,then ?
better to store image in database as binary and retrive and save in temp folder and show in picture box
Vishal Pand3y 20-Dec-13 5:17am    
@Aravindba performance wise storing image's path will be better. there are some disadvantages of it but if you want to store millions of image and storing path will be better
and if you are deleting images from folder then delete it's path from database to
Karthik_Mahalingam 20-Dec-13 5:20am    
true...
@Arvaid , I am not forcing him store in Db or Web server.. it depends on the Images count...
they have to choose which suits for their requirement...
Make one folder images in your web application and take varbinary datatype as variable in your backend(database)
its easy and work smoothly!
C#
protected void Button1_Click(object sender, EventArgs e)
       {
           if (FileUpload1.HasFile)
               try
               {
                   FileUpload1.SaveAs(Server.MapPath("~/your folder name/" + FileUpload1.FileName));
                   Label1.Text = "File name: " +
                        FileUpload1.PostedFile.FileName + "<br>" +
                        FileUpload1.PostedFile.ContentLength + " kb<br>" +
                        "Content type: " +
                        FileUpload1.PostedFile.ContentType;
                   filename.Text = FileUpload1.FileName;
               }
               catch (Exception ex)
               {
                   Label1.Text = "ERROR: " + ex.Message.ToString();
               }
           else
           {
               Label1.Text = "You have not specified a file.";
           }
       }
 
Share this answer
 

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