2
Answers

retrieving image from sqlserver to asp.net gridview

my gridview displays only text not image 
 
my cs file is
 
private void BindImageGridData()
{
SqlCommand command = new SqlCommand("select empcode,EmpPhoto from tblEmpImg where empcode='"+txtempcode.Text+"'", con);
SqlDataAdapter daimages = new SqlDataAdapter(command);
DataTable dt = new DataTable();
daimages.Fill(dt);
grdEmployee.DataSource = dt;
grdEmployee.DataBind();
}
 
 
 
and my ImageHandler.ashx is
 
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conNatcoSal"].ConnectionString.ToString());
string imageid = context.Request.QueryString["Id"];
con.Open();
// SqlCommand command = new SqlCommand("select Image from Employee where Id=" + imageid, connection);
SqlCommand command = new SqlCommand("select empcode, EmpPhoto from tblEmpImg where empcode="+ 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;
}
}
}
......
finaly aspx page is
 
<div>
<div>
<asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="false">
<Columns>
<%--<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Blood Group" DataField="BloodGroup" /> --%>
<asp:BoundField HeaderText="Emp No" DataField="Empcode" />
<asp:BoundField HeaderText="Image" DataField="Image" Visible="false" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?Id="+Eval("EmpPhoto")%>' Height="150px" Width="150px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
 
 
 
 

Attachment: New_folder.rar

Answers (2)