Introduction
This article explicate the method of inserting images and pictures into SQL Server database table and display it in an Asp.Net GridView control
Description
Have you seen any web application or website without images? No, you cannot. Images played a major role in web application development. Either it's a static html website or an advanced RAD application, everything is build along with images. If your application is an E-Commerce based or Image Gallery portal, definitely you have to suffer lot on saving the images in different location with different sizes and types. And it's not an easiest job to manage those unwanted and outdated images to be removed from your file server, then making backup of those images from one server location to another location. So it is clearly time consuming and hectic.
To make your task easier, this article explains you the methods of storing the images into data source. There are many advantages of saving the images into database. The main advantage is easy management of images. You can control the number and size of images stored in your server. You can remove all unnecessary images from the database in a single sql query and you can backup the image data easily. On the other hand, you should be generous of keeping sufficient memory store in your database server.
(Optional) Is there any background to this article that may be useful such as an introduction to the basic ideas presented?
Using the code
A brief description of how to use the article or code.
Stream img_strm=file1.PostedFile.InputStream;
int img_len=file1.PostedFile.ContentLength;
string strType=file1.PostedFile.ContentType;
string strType1=file1.Type;
string strName=txtName.Text;
Byte[] imgData = new byte[img_len];
int n = img_strm.Read(imgData,0,img_len);
int result = savetodb(strName,imgData,strType);
<hr />
private int savetodb(string strName,byte[] imgData,string strType)
{ int return1=0;
try
{
SqlCommand storeimage = new SqlCommand("INSERT INTO image1(id,img_len,imgtype,bytles) values (@id,@img_len,@imgtype,@bytles)", con);
storeimage.Parameters.Add("@id", SqlDbType.VarChar).Value = strName;
storeimage.Parameters.Add("@img_len", SqlDbType.BigInt).Value = imgData.Length;
storeimage.Parameters.Add("@imgtype", SqlDbType.VarChar,6).Value= strType;
storeimage.Parameters.Add("@bytles", SqlDbType.Image).Value = imgData;
con.Open();
return1 = storeimage.ExecuteNonQuery();
con.Close();
}
catch(Exception ex){Response.Write(ex);con.Close();}
return return1;
}
the above coding will store the data to database
Console.Write(id);
con.Open();
SqlCommand adp =new SqlCommand("select * from image1 where id='"+id+"'",con);
SqlDataReader dr=adp.ExecuteReader();
dr.Read();
Context.Response.ContentType=dr["imgtype"].ToString();
Context.Response.BinaryWrite((byte[])dr["bytles"]);<hr />
this coding part is use full to get data from database and load it into image
to set it to datalist we have to add
image id="Image2" runat="server" imageurl="%#content.aspx?id="+DataBinder.Eval(Container.DataItem,"id")%" /