hi dear,
first of all device your code into 3 part.
1. design a grid
2. Bind Your Code
3. Implement IHttpHandler for image
1. Design
<asp:templatefield headertext="Photo" xmlns:asp="#unknown">
<itemtemplate>
<asp:image id="empImage" runat="server" imageurl="<%# "ImageHandler.ashx?ImID="+ Eval("ID") %>" width="100" height="120" alternatetext="Student Photo" descriptionurl="~/image/no-image-a.jpg" />
</itemtemplate>
</asp:templatefield>
2. Bind Your Code
private void fillGrid(DataTable dt)
{
myGrid.DataSource = dt;
myGrid.DataBind()
}
3. Implement IHttpHandler for image
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string imageid = Convert.ToString(context.Request.QueryString["ImID"]);
if (imageid != null && imageid.Trim() != "")
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString());
connection.Open();
SqlCommand command = new SqlCommand("select PHOTO from EmpMst where ID=" + imageid, connection);
SqlDataReader dr = command.ExecuteReader();
dr.Read();
try
{
context.Response.BinaryWrite((Byte[])dr[0]);
connection.Close();
}
catch
{
Byte[] b = File.ReadAllBytes(context.Server.MapPath("~/image/no-image-a.jpg"));
context.Response.BinaryWrite(b);
connection.Close();
context.Response.End();
}
}
else
{
Byte[] b = File.ReadAllBytes(context.Server.MapPath("~/image/no-image-a.jpg"));
context.Response.BinaryWrite(b);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}