Home  »  Articles  »  ASP.Net  »

How To Save and Retrive Image into/from SQL Server Database by using ASP.Net & C#.Net

 Posted By : Manoranjan SahooPosted Date : 15/10/2010 10:25:59 AM Category : ASP.Net Points : 6 (Rs 3.00)

Store Image into Database

Here i put one sample code to do so.
i used one Fileupload control to select the image.

Steps :

1. Create one Fileuploader in ASP.Net webpage
2. Then Place one Button onto the Page.
3. Then Add the following code to the Click Event of that Button.

Before all this create one database, and one table with the name ImageTable and the following field.
FieldName DataType
id int
img Image
Description varchar(200)
width varchar(10)
height varchar(10)

Then Put this code and execute:

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.PostedFile!=null && FileUpload1.PostedFile.FileName != "")
    {
        byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile Image = FileUpload1.PostedFile;
        Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);
        SqlConnection myConnection = new SqlConnection("Put your Connection string");
        SqlCommand storeimage = new SqlCommand("insert into  ImageTable (img,description,width,height) values(" + "@image,'Description',@imagesize,@imageheight)", myConnection);
        storeimage.Parameters.Add("@image", SqlDbType.Image, myimage.Length).Value = myimage;
        System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
        storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = img.Width;
        storeimage.Parameters.Add("@imageheight", SqlDbType.BigInt, 99999).Value = img.Height;
        myConnection.Open();
        storeimage.ExecuteNonQuery();
        myConnection.Close();
    }
}

Retrieve The Image From Database

To Use Handler for displaying image any where, use the following code

Steps

1. Create one Handler.ashx file . To create one Handler file RightClick on the projectName in the solution explorer, click on the Add New Item then select Generic Handler then click on OK button
2. Place the following code in the “public class Handler : IHttpHandler {”

public void ProcessRequest (HttpContext context)
{
    SqlConnection myConnection = new SqlConnection("Put Connection string Here");
    if (myConnection.State == ConnectionState.Closed)
    {
       myConnection.Open();
    }
    string sql = "Select img from ImageDataBase where id="+context.Request.QueryString["id"]+"";
    SqlCommand cmd = new SqlCommand(sql, myConnection);
    cmd.Prepare();
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();
    context.Response.BinaryWrite((byte[])dr["img"]);
    dr.Close();
    if (myConnection.State == ConnectionState.Open)
    {
        myConnection.Close();
    }
}
 
public bool IsReusable
{
    get
    {
        return false;
    }
}

Now use the following method to call the handler to display.
Suppose you want to display in an Image box control of ASP.Net then follow the steps.
1. Create one Image Control of ASP.Net
2. Then set the image url of that as “Image1.ImageUrl = “Handler2.ashx?id=1” . you can set here the id number or unique number corresponding to that image.Based on this the query search for the image from the database.

Thank You.

Responses
No responses found for this thread. Be the first to respond this thread.
Post Reply
Login to post Response.