473,414 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Cannot retrieve images from SQL Server

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.


Nov 18 '05 #1
3 2390
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/de...BytesTopic.asp

and
http://msdn.microsoft.com/library/de...omdatabase.asp
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
<-> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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.


Nov 18 '05 #2
Still not quite sure how to do it!

"Mark Fitzpatrick" <ma******@fitzme.com> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...
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/de...BytesTopic.asp
and
http://msdn.microsoft.com/library/de...omdatabase.asp

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
<-> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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.



Nov 18 '05 #3
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)

<-> wrote in message news:#w**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael Walton | last post by:
I need some help, because I am utterly confused with how to do this. I have 2 things that I need to figure out, and could use some sample code or just some general direction: 1) I need to build...
10
by: mike | last post by:
regards: I use Jtidy (api) to translate a HTML file into a "XHTML file". But The "XHTML file" cannot be identified by nokia 6600. Do I miss something important? Or this is Jtidy's weakness or...
5
by: Jim Richards | last post by:
Hello all. I created the "student2" user account and set the password to "student2". I then went to Enterprise Manager and in the class_mgr database and in its customers table, I gave this...
7
by: fahdsultan | last post by:
A very small percentage of my visitors have complained that they cannot see the images on my site. This is a fabric site, and the pictures of the swatches are generated with ImageMagik, the rest of...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
1
by: Luis Esteban Valencia | last post by:
How to retrieve images from sql server? -- LUIS ESTEBAN VALENCIA MICROSOFT DCE 2.
0
by: dfetrow410 | last post by:
I am using a datalist. I need to retrieve only selected values. I tried to use the following code below with no luck. Not sure how to do the backend <tr> <td align="left" valign="top"><img...
1
by: Cem | last post by:
Hi, in GridView1 I have following code: <ItemTemplate> <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("airport_active", "images/icons/ico_airport_active_{0}.gif") %>'...
1
by: =?Utf-8?B?RGFuaWVsIERpVml0YQ==?= | last post by:
First, I am using VS2005 with windows XP. I am having an issue where I cannot see the images in my usercontrols or the ASPX pages they are dropped onto (the ASPX pages are on the root of the site...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.