0

my pictures are stored in my database as binary data.

How can I show them in my itemtemplate tag in asp?

I established the network and everything needed.

for string data I'm using the following tags

<ItemTemplate>
                         <asp:Label ID="lblProvider" runat="server" Text='<%#Eval("Provider") %>' />
                    </ItemTemplate>

my column in the database is called Provider.

Now for the picture part, the column is called Picture,

this is ofcourse not working

 <asp:TemplateField HeaderText="Picture" SortExpression="Picture" >
                    <ItemTemplate>
                        <asp:Label ID="lblimage" runat="server" Text='<%#Eval("Picture") %>' />
                     </ItemTemplate>
              </asp:TemplateField>

How can I show the picture in the gridview?

1 Answers1

1

You can create a HTTP handler for this. And use that handler as image source in itemtemplate. It would look like this.

<ItemTemplate>
    <img id="Image1" src='PathToHandler.ashx?id=<%#Eval("id")%>' alt="img"/>
</ItemTemplate>

Note: I am trying to pass primary key as id to handler, so that in handler you can retrive binary data using id from SQL table.

And in handler, use DB call to get binary data from SQL server using id in where condition. And write that to Response.OutputStream. Use following for response write a binary data ( image).

using (MemoryStream ms = new MemoryStream())
{
    // fill the memory stream and then write.
    ms.WriteTo(Response.OutputStream);
}

OR

I have already answered this similar type of question (but with different approach) - ASP.NET C# Get retrieve and show image from SQL Server database

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • this is not working, i created the handler, but for a reason i cant' pass eval, i put ImageUrl='PathToHandler.ashx?id=3' and it worked so my handler is working but i can't pass the id. in my handler, context.Request.QueryString("ImID").ToString() is returning "<%".. this is my error I'm trying to check my problem – Fares Joseph Eid Oct 24 '14 at 10:48
  • UPDATE : this is not working, i created the handler, but for a reason i cant' pass eval, i put ImageUrl='PathToHandler.ashx?ImID=3' and it worked so my handler is working but i can't pass the id. in my handler, context.Request.QueryString("ImID").ToString() is returning "<%".. this is my error I'm trying to check my problem – Fares Joseph Eid Oct 24 '14 at 10:49
  • Got it @FaresJosephEid , you can not pass `<% .. %>` to server control, need to discard that and use simple html control. I have updated my post, please use that – Arindam Nayak Oct 24 '14 at 10:55