469,926 Members | 1,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

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 6260
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Tan | last post: by
4 posts views Thread by Guy | last post: by
3 posts views Thread by AG | last post: by
4 posts views Thread by seth_hickel | last post: by
4 posts views Thread by glbdev | last post: by
1 post views Thread by Jeff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.