Store and retrieve images from database using

Store and retrieve images from database using

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..... 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("@img", byt);
// 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)
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);

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

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)

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


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

Store and Retrieve the images from sqlserver using

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: