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;
}
Store and Retrieve the images from sqlserver using c#.net