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
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;
}
}
}

AniPosted Mar 25, 2016, 8:51 AM
Hi Saafia,
I guess command should be changed like below
Ani
Vinodh NarayananPosted Mar 25, 2016, 8:12 AM