Image binding

G

GD

I'm trying to bind (display) a SQL column to an <asp:image control. I was
under the impression that this could be achieved my pointing the ImageURL to
the databound column but it doesn't seem to work.

If I view the page source I can see that the ImageURL property has the value
'System.Byte[]' but no image is displayed.

By placing a STOP in the _ItemDatabound event I can see that the column does
contain data (DataBinder.Eval(e.Item.DataItem, "ThumbImage"))

Has anyone come across this before ?

Thanks

Gary


<asp:DataList id="ProductList" RepeatColumns="1" runat="server">
<ItemTemplate>
...
<asp:Image runat="server" id="imgPhoto" Visible="True"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ThumbImage")%>'
width="100" height="100"></asp:Image>
...
</ItemTemplate>
</asp:DataList>
 
M

Mark Fitzpatrick

You can't do it this way. You can't bind directly to a binary object from a
database. You need to have a seperate page generate the image that way the
browser will see it as an image file even though it is an asp.net page.
Usually this is done by passing in an ID as a querystring for the generator
page such as: imageGenerator.aspx?id=1. That page will then fetch the
particular image byte object from the database and dump it to the output
stream. In the page that calls it through an image element or asp:image
control it will be rendered as an image.
 
G

GD

Thanks Mark

and for anyone else with the same problem .....

The getImage.aspx code is ...
Dim nPhotoId As Integer = CInt(Request.QueryString("PhotoId"))

Dim Dr As SqlClient.SqlDataReader = GetPhoto(nPhotoId) ' Open a connection
and run the stored proc that returns a photo

Response.Expires = 0

Response.Buffer = True

Response.Clear()

While Dr.Read

Response.BinaryWrite(Dr("Photo"))

End While

Response.End()

The binding code is

<asp:Image runat="server" id="imgPhoto" Visible="True" ImageUrl='<%#
"getImage.aspx?PhotoId=" & DataBinder.Eval(Container.DataItem,
"ThumbPhotoId")%>' width="80" height="80"></asp:Image>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top