I have writtten some image upload code in which image is stored successfully in binary format in a database.
My problem is that image is not displayed in the gridview
This is my database table Pictures
ImgId int
ImageName varchar(500)
Image image
Gridview.aspx
<asp:GridView ID="GridView1" runat="server" Style="z-index: 103; left: 232px; position: absolute;
top: 225px" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "ImageHandler.ashx?id="+ Eval("ImgId") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView.cs
public void Load_GridData()
{
con.Open();
string sql = "Select ImgId,Id,ImageName,Image from Pictures";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
GridView1.Attributes.Add("bordercolor", "black");
}
Handler.ascx
using System;
using System.Web;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\omar\Documents\Visual Studio 2005\WebSites\AJAXEnabledWebSite7\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
SqlCommand com = new SqlCommand("Select Image From Pictures Where ImgId=@imgid", con);
com.Parameters.Add("@imgid", System.Data.SqlDbType.Int).Value = context.Request.QueryString["id"];
con.Open();
SqlDataReader dReader = com.ExecuteReader();
dReader.Read();
com.Prepare();
context.Response.BinaryWrite((byte[])dReader["Image"]);
con.Close();
}
public bool IsReusable {
get {
return false;
}
}
Is the problem with the Handler or Gridview?