Cannot retrieve images from SQL Server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Still not quite sure how to do it!

Mark Fitzpatrick said:
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/d...aSqlClientSqlDataReaderClassGetBytesTopic.asp
http://msdn.microsoft.com/library/d...html/cpconobtainingblobvaluesfromdatabase.asp


Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage


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.
 
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)
 
Back
Top