Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1.Database table - tbl_image
id int
name nvarchar(50)
picture image

2.ASPX html page
ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                                                               <Columns>
                                                                   <asp:BoundField DataField="id" HeaderText="Id" />
                                                                   <asp:BoundField DataField="name" HeaderText="Name" />
                                                                   <asp:TemplateField HeaderText="Picture">
                                                                   <ItemTemplate>
                                                       <img src="Handler1.ashx?Id=<%# Eval("id").ToString() %>" width="150" height="100" />
                                                                       </ItemTemplate>

                                                                   </asp:TemplateField>
                                                               </Columns>
                                                           </asp:GridView>



3.ASPX coding page
C#
protected void ShowFromDatabase(object sender, EventArgs e)
        {
            string str2 = "select id,name from tbl_image";
            obj.dr = obj.ret_dr(str2);
            GridView1.DataSource = obj.dr;
            GridView1.DataBind();
        }


4.Handler1.ashx page
C#
public class Handler1 : IHttpHandler
   {
       Class1 obj = new Class1();
       public void ProcessRequest(HttpContext context)
       {
           //context.Response.ContentType = "text/plain";
           //context.Response.Write("Hello World");
           string pid=context.Request.QueryString["Id"];
           string str = "select picture from tbl_image where id='"+pid+"'";
           obj.dr = obj.ret_dr(str);
           while (obj.dr.Read())
           {
               context.Response.ContentType = "image/jpg";
               //context.Response.BinaryWrite((Byte[])obj.dr[obj.dr.GetOrdinal("picture")]);
               context.Response.BinaryWrite((Byte[])obj.dr["picture"]);
           }
       }

       public bool IsReusable
       {
           get
           {
               return true;
           }
       }
   }


These are my four pages.If someone can correct it,then kindly reply me

What I have tried:

I tried with these four pages.In output window id,name is displayed but picture is not there,only cross symbol
Posted
Comments
King Fisher 2-Dec-16 2:44am    
getting any exception ? refer this piece of code
Member 11873864 2-Dec-16 4:55am    
No any exception.But picture is coming with cross symbol.
Member 11873864 2-Dec-16 4:58am    
Thanks for reply.I want to display all images from database on one click not only single image.
Member 11873864 2-Dec-16 6:05am    
I used your referred code but still problem is same.
Member 11873864 2-Dec-16 6:13am    
Is it necessary to save the image in .net folder also?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900