Groups | Blog | Home
all groups > asp.net > november 2003 >

asp.net : Cannot retrieve images from SQL Server



bruce barker
11/21/2003 5:23:50 PM
you have two problems here. The datareader returns a SqlBinary, which you
need to convert to byte[]

Response.BinaryWrite((byte[]) dr["Image"]);


IE unlike other html 4.0 browsers, does not support inline binary images
(which you would need to encode), so your ImageUrl needs to be an actual URL
that returns an image. you will need to write an aspx page that returns the
image, and referce this aspx page in the grid.


-- bruce (sqlwork.com)





[quoted text, click to view]

Mark Fitzpatrick
11/21/2003 5:24:35 PM
If you're using ASP.Net 1.1, the SqlDataReader class has a method called
GetBytes. One of the parameters you pass to this function is a byte array,
which can then be output to the browser in the response.binarywrite method.

Check out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataSqlClientSqlDataReaderClassGetBytesTopic.asp

and
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconobtainingblobvaluesfromdatabase.asp


Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage


[quoted text, click to view]

<->
11/21/2003 10:42:07 PM
Hi,

I have a SQL Server database table where one of the columns is of type
Image.
The problem occurs when I try to retrieve images from the table. I'm using
the following C# code:


SqlDataReader dr = sqlCommand.ExecuteReader();

while (dr.Read())

{

Response.BinaryWrite(dr["Image"]);

}

sqlConnection.Close();



The error I'm getting is that dr["Image"] is of type object whereas the
BinaryWrite method requires a parameter of type Byte[] - How can I
convert/cast dr["Image"] into Byte[] ?



Also, I have set up a template column on a DataGrid to display images using:



<asp:TemplateColumn HeaderText="Image">

<ItemTemplate>

<asp:Image id=Image runat="server" ImageUrl='<%#
DataBinder.Eval(Container.DataItem, "Image") %>'>

</asp:Image>

</ItemTemplate>

</asp:TemplateColumn>



But can't seem to bind the images to the DataGrid - the images are reading
"System.Byte[]"

How can I cast/convert or read the Byte[] array of image binary data into
images?



Thanks,

Steve.





<->
11/22/2003 12:52:45 AM
Still not quite sure how to do it!

[quoted text, click to view]

AddThis Social Bookmark Button