Saafia NM

Saafia NM

  • NA
  • 52
  • 11.2k

Display Image from Database using .ashx handler

Mar 25 2016 7:44 AM
I am trying to display an uploaded image from database using .ashx handler. I got the image from database in gridview, but only first image in table is displayed. For the second row the same image is repeating .What to do?
 
Default.aspx
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?ImID="+ Eval("uname") %>' Height="50px" Width="50px" DataField="Image" />
 
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = Convert.ToString(Session["uname"]);
string uname = Label1.Text; 
con.Open();
SqlCommand command = new SqlCommand("SELECT * from Product", con);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Attributes.Add("bordercolor", "white");
con.Close();
}
}
 
imagehandler.ashx 
<%@ WebHandler Language="C#" Class="imagehandler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class imagehandler : IHttpHandler
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Auction.mdf;Integrated Security=True;User Instance=True");
public void ProcessRequest (HttpContext context)
{
string imageid = context.Request.QueryString["ImID"];
con.Open();
SqlCommand command = new SqlCommand("select Image from Product where uname='"+imageid+"' ", con);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
context.Response.BinaryWrite((Byte[])dr[0]);
con.Close();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
 
 
 

Answers (2)