Store and retrieve images from database using Asp.net


Store and retrieve images from database using Asp.net....

First create the table in sqlserver like below

create table imgtable(imgid numeric,img image)

after create the table open vs 2008. click file->New->Website then place one text box, one file upload control, two button(one for save another one for retrieve), one image control from tool box....

...Give the Name for controls.....
...save button name is "btnsave", TextBox name is "txtid", fileupload control name is "picture" retrieve button name is "btnshow"....
...then double click the save button and type the following code....


protected void btnsave_Click(object sender, EventArgs e)
{
FileUpload img = (FileUpload)imgupload;
Byte[] byt = null;
if (img.HasFile && img.PostedFile != null)
{
HttpPostedFile File = imgupload.PostedFile;
byt = new Byte[File.ContentLength];
File.InputStream.Read(byt, 0, File.ContentLength);
SqlCommand cmd = new SqlCommand("insert into imgtable values(@id,@img)",Class1.conn());
cmd.Parameters.AddWithValue("@id",txtid.Text);
cmd.Parameters.AddWithValue("@img", byt);
cmd.ExecuteNonQuery();
// if you want to see image instantly after save use the below single line code
picture.ImageUrl = "~/ShowImage.ashx?id=" + txtid.Text;

}

}


then go to solution explorer then right click project name->Add New Item->Generic page then type the file name as "ShowImage.ashx" and place the following code inside the "ShowImage.ashx" then place the following code inside the code editor in "ShowImage.ashx" page..


<%@ WebHandler Language="C#" Class="ShowImage" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public class ShowImage : IHttpHandler {

public void ProcessRequest (HttpContext context)
{
string id;
if (context.Request.QueryString["id"] != null)
id=context.Request.QueryString["id"];
else
throw new ArgumentException("No parameter specified");
context.Response.ContentType = "image/jpeg";
context.Response.ContentType = "image/bmp";
Stream strm = ShowEmpImage(id);
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);

while (byteSeq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteSeq);
byteSeq = strm.Read(buffer, 0, 4096);
}
//context.Response.BinaryWrite(buffer);

}
public Stream ShowEmpImage(string id)
{
SqlConnection con = new SqlConnection("server=.; integrated security=true; initial catalog=master");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT img FROM imgtable WHERE imgid ='" + id + "'",con);
object img = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])img);
}
catch
{
return null;
}
finally
{
con.Close();
}
}

public bool IsReusable {
get {
return false;
}
}

}


if you want to retrieve the image from sqlserver using id just type the following code inside Retrieve button


protected void btnshow_Click(object sender, EventArgs e)
{
picture.ImageUrl="~/ShowImage.ashx?id="+txtid.Text;
}


Related Articles

Image Handling

This article describes about the storing photos into a database and retrive the stored photos from a database.

More articles: Save images in database Retrieve images from database

Comments

Author: Rajesh23 Apr 2010 Member Level: Bronze   Points : 0

Store and Retrieve the images from sqlserver using c#.net

Author: Manigandan26 Apr 2010 Member Level: Gold   Points : 0

Nice Article Rajesh

But dont do self compliments :-)

Author: Karthik26 Apr 2010 Member Level: Bronze   Points : 0

nice rajesh...keep on posting

Author: saravana26 Apr 2010 Member Level: Bronze   Points : 0

its good..thanks for share with us...

Author: manisha05 May 2010 Member Level: Gold   Points : 0

nice article and thanx for sharing with us.

-- Manisha Chaubey

Author: Nazeeba28 Jun 2010 Member Level: Bronze   Points : 1

the code posted for image storing & retrieval from database was very useful for my appln development... thanks... keep on posting...



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: