473,387 Members | 1,497 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,387 software developers and data experts.

How to Display an image, from a DB, into the Gridview (ASP.NET 2.0)

Hello,

I would like to know how do display an image into a Gridview (ASP.NET 2.0)
taken directly from a DataBase, NOT using an URL, like described in this
article:

http://msdn.microsoft.com/library/de...idViewEx06.asp

I have NO URL, i have the binary image inside the DB, like the field "Photo"
in the table "Employees" of the "NWIND.MDB" DataBase.
Any hints?

Thanks,
Auto.
Apr 26 '06 #1
18 6481
You create an aspx that opens the image from the database, changes the
response type and response.write() the image. (Another option that is
commonly used is to use a HTTPHandler to return back the image)

You then place a link to this aspx page for your image as if it was a normal
image.

http://www.aspfree.com/c/a/ASP.NET/R...--C---Part-II/
is an example.

Cheers,

Greg
"Auto" <Au**@Auto.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hello,

I would like to know how do display an image into a Gridview (ASP.NET 2.0)
taken directly from a DataBase, NOT using an URL, like described in this
article:

http://msdn.microsoft.com/library/de...idViewEx06.asp

I have NO URL, i have the binary image inside the DB, like the field
"Photo"
in the table "Employees" of the "NWIND.MDB" DataBase.
Any hints?

Thanks,
Auto.

Apr 26 '06 #2
http://www.asp.net/default.aspx?tabindex=5&tabid=41
Personal web starter kit has a http handler that does exactly that. You can
download the starter kit and look at the code.

--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
"Greg Young [MVP]" <Dr*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
You create an aspx that opens the image from the database, changes the
response type and response.write() the image. (Another option that is
commonly used is to use a HTTPHandler to return back the image)

You then place a link to this aspx page for your image as if it was a
normal image.

http://www.aspfree.com/c/a/ASP.NET/R...--C---Part-II/
is an example.

Cheers,

Greg
"Auto" <Au**@Auto.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hello,

I would like to know how do display an image into a Gridview (ASP.NET
2.0)
taken directly from a DataBase, NOT using an URL, like described in this
article:

http://msdn.microsoft.com/library/de...idViewEx06.asp

I have NO URL, i have the binary image inside the DB, like the field
"Photo"
in the table "Employees" of the "NWIND.MDB" DataBase.
Any hints?

Thanks,
Auto.


Apr 26 '06 #3
Thanks for the answer, but i don't want the photo in the page
(Response.BinaryWrite) i want it in a Gridview column.

Regards.
"Greg Young [MVP]" <Dr*************@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
You create an aspx that opens the image from the database, changes the
response type and response.write() the image. (Another option that is
commonly used is to use a HTTPHandler to return back the image)

You then place a link to this aspx page for your image as if it was a normal image.

http://www.aspfree.com/c/a/ASP.NET/R...--C---Part-II/ is an example.

Cheers,

Greg
"Auto" <Au**@Auto.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hello,

I would like to know how do display an image into a Gridview (ASP.NET 2.0) taken directly from a DataBase, NOT using an URL, like described in this
article:

http://msdn.microsoft.com/library/de...idViewEx06.asp
I have NO URL, i have the binary image inside the DB, like the field
"Photo"
in the table "Employees" of the "NWIND.MDB" DataBase.
Any hints?

Thanks,
Auto.


Apr 27 '06 #4
It's very easy. The code that you have seen in that urls write the
image in the page, isn't it? Well, use that page as source of an <img>
html control. For example:

<img src="generateimage.aspx?idimage=3" />

Just add that line in the gridview column template. In the
generetaimage.aspx page put the code with the "Response.BinaryWrite".
Thats all! If you need to load an specific image, pass parameters (like
in the example) to the page, and use it to filter the SQL query.

Bye!

Apr 27 '06 #5
By the way, I think that the example you have mentioned before do the
same that I have said. It's the only way to do this. I don't understand
why you can not use an imagefield or img control inside a column
template.

Apr 27 '06 #6

"the4man" <th*****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
By the way, I think that the example you have mentioned before do the
same that I have said. It's the only way to do this. I don't understand
why you can not use an imagefield or img control inside a column
template.


I would like to build a web monitor application that show a Gridview and
refresh its contents every one or two seconds.
I was able to implement that in ASP.NET 2.0, using ATLAS with a
TimerControl, ScriptManager and an UpdatePanel.
In each row i have an image (not big) that i would like to show, but i don't
see an ease way as for the other fields.
I'm realizing that i could load the image, using your tip, only when the
page loads, not every one or two seconds in the postback event,
since the images don't change as the other data does. That's way i was
looking for an easy way to load the binary image from the DB.

I know that in the beta version there was this possibility: bind directly
the binary field of the image to the gridview column.

Regards,

Auto.
Apr 27 '06 #7
Well, I think that behaviour (bind an image to a column) is still
available in the final version too (the first url that you sent use
that method).

Using "my" method, that is, converting a column into a template field,
then putting inside the <img> tag should work. Every time the gridview
is refreshed the img tag is regenerated too. If the image has changed
it will be shown in screen....if you use a little "trick". The url
should be like this: showimage.aspx?idimage=XXX&randonparameter=121232.
The "randonparameter" is just to avoid browsers cache the image.

I hope this help you. If not...post your code just to have a look at it.

Apr 28 '06 #8
I know that in the beta version there was this possibility: bind directly
the binary field of the image to the gridview column.


This is what i was speaking about:
http://www.codersource.net/asp_net_gridviewcontrol.aspx
(Look at the picture of the form with the caption "Add Field")

Regards,

Auto.
Apr 28 '06 #9
That is still available in the final version of Vs 2005.
http://msdn.microsoft.com/library/de...idViewEx06.asp

Apr 28 '06 #10
Please, let it alone...

"the4man" <th*****@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
That is still available in the final version of Vs 2005.
http://msdn.microsoft.com/library/de...idViewEx06.asp

Apr 28 '06 #11
I have the binary image in my hand...
No, i need the following steps to get the same:

1) Reopen the DB
2) Run the query
3) Output the image
4) Close the DB.

Take it easy, please.
Apr 28 '06 #12
I'm trying to help you the best I can. I'm sorry, you were right. The
imgfield of gridviews are based in urls, not binary data. So is not
valid for you. Sorry again. Then, I think, the only way is doing what I
said: a template field with a dinamicly generated url. I use a similar
mechanism in one of the web sites to show images from DB (but with an
asp:Repeater instead of an asp:GridView). There is no problems to do
that with GridViews, as i said before. Every time you refresh the
gridview, the image will be reload. Just add a random parameter in the
url, to avoid that IE catch the image.

At this moment I don't see any other solution. That don't mean it
doesn't exists. If you need more help from me, please tell me. If not,
I will "let it alone" (I'm not very sure what that expresion means,
because I'm not native, but more or less...)

Apr 28 '06 #13
Thank you for your help.

What i was saying was that in the ASP.NET 2.0 beta version there was the
possibility to bind a binary image from a DB, directly to the gridview, or
using the URL. In the final version thay left only the URL mode. Please,
don't say it's the same or this is not true.
Also i told you, i refresh the gridview every one or two seconds.
If i have 30 rows already in the Dataset, i need to show them as soon as
possible.
It's not good, FOR EACH ROW

1) Reopen the DB
2) Do the query
3) Output the image
4) Close the DB

when i have ALREAY all the data i need (photo included) in the Dataset.

Since the images don't change, i thought to load them in the image fields of
the gridview - using the URL as you told me -only when the page loads, not
for the post back every one or two seconds. For each post back i'm going to
refresh the data of the gridview, but the photos.

Regards.

Auto.
Apr 29 '06 #14
The bandwidth between the dataset and the DB is usually of less concern than
the bandwidth between the dataset and the browser. They may be using diaup
or other slow connectivity methods.

How can the browser know what the bits are if it does not recieve MIME
information with the image. I thought that the browser had to know what the
bits were in order to interpret them for the user on the other end of the
browser.

This allows browsers to do stuff like, retrieve the text and slowly fill in
the images asynchronously and if the user decides to browse away in the
interim (before all of the images arrive) then that is his/her choice. The
server doesn't have to send the browser the files not requested. This would
save bandwidth.

The gridview is html/text (or something like that) so if you send the image
as part of that stream you could get some really cool bytes on the screen
but you are thinking that the browser knows that these bytes were a "png" or
a "jpg" or a "gif" or a "bmp" (or some other?). file that you uploaded into
a field.

Whenever I've stored images in a database I've had to store the "mime type"
in a field and the size in bytes (so the browser knows I've sent it all now
.... stop making the Netscape Icon animate).

Remember that you can only retrive images from SQL server (I don't think
that MSAccess can do this)

What about serving up "filename.doc" files. Then it all comes as one stream
and you don't have to worry about the complexities of writing an "url" into
an <IMG> tag or other active content issues.

JT

"Auto" <Au**@Auto.com> wrote in message
news:OV**************@TK2MSFTNGP03.phx.gbl...
Thank you for your help.

What i was saying was that in the ASP.NET 2.0 beta version there was the
possibility to bind a binary image from a DB, directly to the gridview, or
using the URL. In the final version thay left only the URL mode. Please,
don't say it's the same or this is not true.
Also i told you, i refresh the gridview every one or two seconds.
If i have 30 rows already in the Dataset, i need to show them as soon as
possible.
It's not good, FOR EACH ROW

1) Reopen the DB
2) Do the query
3) Output the image
4) Close the DB

when i have ALREAY all the data i need (photo included) in the Dataset.

Since the images don't change, i thought to load them in the image fields
of the gridview - using the URL as you told me -only when the page loads,
not for the post back every one or two seconds. For each post back i'm
going to refresh the data of the gridview, but the photos.

Regards.

Auto.

Apr 29 '06 #15
Ok, I understand you. I said that was "the same" because I red the
article very quickly. It was one of that times that you see something
clear....even when is not true. :P. But yes, you are right, that option
doesn't exists in the final versión.

If you want to show the images just in page load, not in every refresh,
to reduce database access, try with my method again.....but WITHOUT the
"random" parameter. The browser will cache the images so won't have
calls to the DB (because won't call the createimage.aspx page). And you
can use server cache too. The page which generate the images can be
cached in server, just add an OutPutCache directive with a varybyparam
option. This will reduce access to the DB, and it always works (the
browser cache can be disabled by users). Another tip: add to the URL
the image width and height. For example:

<img src="createimage.aspx?image=10" width="100" height="60" />

With theese data the browse can "reserve" space in the page for the
images, and will show the texts as soon as posible, even if the images
aren't download yet.

I hope this help you.

By the way: yes, it's possible to retrive images from a MSAccess DB.

Apr 30 '06 #16

"J. Toop" <ba******@online.nospam> wrote in message
news:uI******************@TK2MSFTNGP03.phx.gbl...
The bandwidth between the dataset and the DB is usually of less concern
than the bandwidth between the dataset and the browser. They may be using
diaup or other slow connectivity methods.
OK.
Anyway the image must arrive to the browser, isn't it ?
How can the browser know what the bits are if it does not recieve MIME
information with the image. I thought that the browser had to know what
the bits were in order to interpret them for the user on the other end of
the browser.
OK, good point.
I thought the imagefield was so smart to look at the header of the image
stream (binary in the DB) to understend what kind of images it was.

This allows browsers to do stuff like, retrieve the text and slowly fill
in the images asynchronously and if the user decides to browse away in the
interim (before all of the images arrive) then that is his/her choice. The
server doesn't have to send the browser the files not requested. This
would save bandwidth.
OK, good point.

The gridview is html/text (or something like that) so if you send the
image as part of that stream you could get some really cool bytes on the
screen but you are thinking that the browser knows that these bytes were a
"png" or a "jpg" or a "gif" or a "bmp" (or some other?). file that you
uploaded into a field.
Yes, i hope so, but maybe i'm realizing it is not.

Whenever I've stored images in a database I've had to store the "mime
type" in a field and the size in bytes (so the browser knows I've sent it
all now ... stop making the Netscape Icon animate).
The size in byte could be known from the DB Field.
I worked with SQL Server and DB with Images.
I was able to know the size by the DB field they were stored,
and also i was able to show them feeding the leadtools control.

Remember that you can only retrive images from SQL server (I don't think
that MSAccess can do this)
I think yes, i have a sample it does.

------------------------------------------------
string idx;

idx = Request.QueryString["idx"];
if (idx == null)
idx = "1";

OleDbConnection myConn = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
Server.MapPath("App_Data\\Photos.mdb"));
myConn.Open();
OleDbCommand _OleCmd = new OleDbCommand("SELECT * FROM Tabella1 WHERE ID = "
+ idx, myConn);
OleDbDataReader dr = _OleCmd.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.ClearContent();
Response.ContentType = dr["ContentType"].ToString();
Response.BinaryWrite((byte[])dr["Photo"]);
}
else
Response.Write("NOT FOUND!");

if (myConn.State != System.Data.ConnectionState.Closed)
myConn.Close();
------------------------------------------------

The access db has stored also the content type.

What about serving up "filename.doc" files. Then it all comes as one
stream and you don't have to worry about the complexities of writing an
"url" into an <IMG> tag or other active content issues.


How can i get a .doc file form an image stored in a DB ?
I can try for learning purpose, but i don't think this is usefull for my
monitoring application.

Thank you.

Auto.

Apr 30 '06 #17
I had a smart idea :-)

I can do this query in order to get the URL of the Image directly from the
DB:

"SELECT *, 'ShowPhoto.aspx?idx=' + cast(EmployeeID AS nvarchar(7)) AS
PhotoURL FROM Employees"

and bind it to an image field in the gridview as shown in the article in my
first post.
Anyway, How can i know which ContentType the photo has ?

In the Northwind DB the emplyee's photo is a BMP, but i if i set the
ContentType of the page = "image/bmp", i don't see any photo.
I tried with a different DB that has jpg photo and it works.

So, any help with emplyee's photo in the northwind DB ?

Apr 30 '06 #18
The content type is correct. I don't know why the image is not showed.
I will do some test when I arrive at work on monday. At this moment I
don't have the northwind db available.

Bye!

P.S: very good idea!

Apr 30 '06 #19

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

Similar topics

1
by: Tan | last post by:
Hi I am desperate for any help with display image in Gridview I have a gridview contain surname , forename ..... and image. I could not display image as my database store the column image as...
0
by: Joey | last post by:
Hello all, I am trying to read a directory listing into a string and then use that data, along with some other data, to create HTML for display in a gridview row. A sample item in the string...
4
by: Guy | last post by:
I'm a little bit confused as what is the bes way to achieve the following: I have 2 database tables, table 1 product en table 2 image. For every product record there can be 0-1 or more image...
1
by: Auto | last post by:
Hello, I would like to know how do display an image into a Gridview (ASP.NET 2.0) taken directly from a DataBase, NOT using an URL, like described in this article: ...
3
by: AG | last post by:
Below is code (slightly modified and converted to VB) that was provided to me in response to another post. I am using it to demonstrate another problem. In order for paging and other features to...
4
by: seth_hickel | last post by:
With other solutions I would get a recordset, read each record and display data by formating my html as I wanted to display values of each record. I am trying to display data in a three column...
4
by: glbdev | last post by:
Hi, I posted this question yesterday but didn't get the answer I needed. I am DESPERATE to get this working so I'm re-posting it because I don't think I worded it correctly. I have a GridView...
1
by: tuman | last post by:
I am making a project which is related to classified web site. I have used a gridview control. In which put some sqltable data. And i want to show a image which is store in my project folder,...
1
by: Jeff | last post by:
Hi asp.net .20 the markup below gives me compile error: - Element 'Image' is not a known element - Type 'System.Web.UI.WebControls.TemplateField' does not have a public property named...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.