Cannot retrieve images from SQL Server

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.
 
M

Mark Fitzpatrick

G

Guest

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.
 
B

bruce barker

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)
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top