2

I have a SQL Server 2008 Table with an image typed column which I use to store images in (in SQL Image Type).

I have an ASPX page with a listview on it which is bound to a datatable which is filled from a query that includes that image. The value of image field is fetched while the datatable is filled.

Now I want to show the image for each field of that list view. I use <%# Eval("ColumnA") %> like syntax to show texts fetched from that query but I do not know how to show the image from that query. (e.g. using which control? using which syntax?)

Farshid
  • 5,134
  • 9
  • 59
  • 87
  • This link shows an example: http://www.aspsnippets.com/Articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx – Josh Apr 10 '12 at 19:49
  • I am not looking for a solution that includes holding image URL in database instead of image itself. – Farshid Apr 10 '12 at 19:49
  • Thanks Josh but I am looking for a way of involving Eval expression of sth like that. – Farshid Apr 10 '12 at 19:51

1 Answers1

2

Try the Data URL scheme:

<img src="<%# ReturnEncodedBase64UTF8(Eval("ColumnA")) %>" />

protected static string ReturnEncodedBase64UTF8(object rawImg)
{
    string img = "data:image/gif;base64,{0}"; //change image type if need be
    byte[] toEncodeAsBytes = (byte[])rawImg;        
    string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
    return String.Format(img, returnValue);
}
rick schott
  • 21,012
  • 5
  • 52
  • 81
  • Unable to cast object of type 'System.Byte[]' to type 'System.String'. – Farshid Apr 10 '12 at 20:12
  • Because I needed to do a : System.Text.Encoding.UTF8.GetBytes((string)rawImg); – Farshid Apr 10 '12 at 20:12
  • Dude, It worked :) You made my day. Thank you very very very much for your help. The problem was going to become a big headache for me. Thank you my friend. – Farshid Apr 10 '12 at 20:20