Image data from Sql server to asp.net

S

seeni

I have tried retrieving the image data from NorthWind database and print in
the asp page using Repeater control. But instead of showing picture, the
browser is showing "System.Byte[]"

My code is
In codebehind file.

private void Page_Load(object sender, System.EventArgs e)
{

if(!Page.IsPostBack)

{

SqlConnection myConnection = new
SqlConnection("server=test2;uid=sa;pwd=;database=NorthWind");

// Connect to the SQL database using a SQL SELECT query to get

// all the data from the "Titles" table.

SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +

" Categories", myConnection);

// Create and fill a DataSet.

DataSet ds = new DataSet();

myCommand.Fill(ds);


Repeater1.DataSource = ds;

Repeater1.DataBind();

}

}

In aspx file

<asp:repeater id="Repeater1" runat="server">
<HeaderTemplate>
<font color="#cc3333" face="verdana" size="3"><b>DataList Control
Header</b> </font>
</HeaderTemplate>
<AlternatingItemTemplate>
<font face="verdana" size="2" color="green">
<br>
<b>Category ID: </b>
<%# DataBinder.Eval(Container.DataItem, "CategoryID") %>
<br>
<b>Category Name: </b>
<%# DataBinder.Eval(Container.DataItem, "CategoryName")%>
<br>
<b>Description: </b>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
<br>
<b>Picture: </b>
<%# DataBinder.Eval(Container.DataItem, "Picture") %>
<p>
</div> </font>
</AlternatingItemTemplate>
<ItemTemplate>
<font face="verdana" size="2">
<br>
<b>Category ID: </b>
<%# DataBinder.Eval(Container.DataItem, "CategoryID") %>
<br>
<b>Category Name: </b>
<%# DataBinder.Eval(Container.DataItem, "CategoryName")%>
<br>
<b>Description: </b>
<%# DataBinder.Eval(Container.DataItem, "Description") %>
<br>
<b>Picture: </b>
<%# DataBinder.Eval(Container.DataItem, "Picture") %>
<p>
</div> </font>
</ItemTemplate>
<FooterTemplate>
<font color="#996600" face="verdana" size="1">DataList Control footer
</font>
</FooterTemplate>
</asp:repeater>

The output is

Category ID: 3
Category Name: Confections
Description: Desserts, candies, and sweet breads
Picture: System.Byte[]


Category ID: 4
Category Name: Dairy Products
Description: Milk and stuff
Picture: System.Byte[]


Category ID: 5
Category Name: Grains/Cereals
Description: Breads, crackers, pasta, and cereal
Picture: System.Byte[]


Category ID: 6
Category Name: Meat/Poultry
Description: Prepared meats
Picture: System.Byte[]


Category ID: 7
Category Name: Produce
Description: Dried fruit and bean curd
Picture: System.Byte[]


Thanks,
-Seeni
 
H

Hans Kesting

seeni said:
I have tried retrieving the image data from NorthWind database and print in
the asp page using Repeater control. But instead of showing picture, the
browser is showing "System.Byte[]"

You can't send image data along with the html code. Image data is retrieved
by the
browser in a separate request when it encounters an <img> tag.

What you need to do is this:
* generate an <img src=...> tag, with a URL that points to a special aspx
file (with some parameters)
* in that aspx, read the image from the database (based on the parameters)
* set an appropriate ContentType ("image/gif", "image/jpeg") and use
Response.BinaryWrite
to write the data.

Hans Kesting
 
D

Dave Rothgery

Hans Kesting said:
seeni said:
I have tried retrieving the image data from NorthWind database and print in
the asp page using Repeater control. But instead of showing picture, the
browser is showing "System.Byte[]"

You can't send image data along with the html code. Image data is retrieved
by the
browser in a separate request when it encounters an <img> tag.

What you need to do is this:
* generate an <img src=...> tag, with a URL that points to a special aspx
file (with some parameters)
* in that aspx, read the image from the database (based on the parameters)
* set an appropriate ContentType ("image/gif", "image/jpeg") and use
Response.BinaryWrite
to write the data.

Just as an FYI, according to the demos and previews I've seen, there will be
a "DynamicImage" control in the next version of ASP.NET which can be bound
to a binary column in a database. Not all that useful for now, but it's
something to think about in the long run.
 
B

Bob Brunton

Well if microsoft put out a Dynamic Image control they are wasting
their time because it already exists.

I have used this control for 6 months now and I don't know anything
about HTTP Handlers or sending stuff other than HTML down the HTTP
response.

Go to the Microsoft ASP.NET Control Gallery.

http://www.asp.net/ControlGallery/ControlDetail.aspx?Control=773&tabindex=2

Regards Bob


Dave Rothgery said:
Hans Kesting said:
seeni said:
I have tried retrieving the image data from NorthWind database and print in
the asp page using Repeater control. But instead of showing picture, the
browser is showing "System.Byte[]"

You can't send image data along with the html code. Image data is retrieved
by the
browser in a separate request when it encounters an <img> tag.

What you need to do is this:
* generate an <img src=...> tag, with a URL that points to a special aspx
file (with some parameters)
* in that aspx, read the image from the database (based on the parameters)
* set an appropriate ContentType ("image/gif", "image/jpeg") and use
Response.BinaryWrite
to write the data.

Just as an FYI, according to the demos and previews I've seen, there will be
a "DynamicImage" control in the next version of ASP.NET which can be bound
to a binary column in a database. Not all that useful for now, but it's
something to think about in the long run.
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top