ASP.Net Stream and Display Image from MS Access Database

by top54u.com 05 Mar, 2008

To retrieve the image from MS Access Database you can pass the select query with image id using OleDbCommand. You can use the following VB code:

 

Dim cmd As New OleDbCommand("select * from tblImg where img_id=@img_id", myAccessConnection)

 

' Mark the Command as a Text

cmd.CommandType = CommandType.Text

 

' Add Parameters to Command

Dim img_id As New OleDbParameter("@img_id", SqlDbType.Int)

img_id.Value = ID

cmd.Parameters.Add(img_id)

Dim myAdapter As New OleDbDataAdapter(cmd) Dim myDataSet As New DataSet

myAdapter.Fill(myDataSet)

 

img_type filed in the Access Database table was used to store the image content type e.g.: image/jpeg, image/gif. Pass this field value to Response.ContentType

Use the Response.BinaryWrite method by passing the binary data retrieved from img_stream field to display the image on web page.

 

Response.ContentType = dRow("img_type")

Response.BinaryWrite(dRow("img_stream"))

 

You can use the following Grid View control to display the image retrieved from the MS Access Database:

 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="5"

GridLines="None" ShowHeader="False">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:Image ID="imgSaved" runat="server" ImageUrl='<%# imageURL( DataBinder.Eval(Container.DataItem, "img_id" )) %>'

AlternateText='<%# DataBinder.Eval( Container.DataItem,"img_title" ) %>' />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

 

Download the free source code here:

AccessImage.zip (135.95 kb)

AccessImage.rar (99.40 kb)

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Comments

4/9/2008 2:14:31 AM

tom

I'm able to upload images to Access using your code, but I'm not able to retrieve images from Access (on page retrieveImages.aspx). the error is

Name 'imageURL' is not declared.

Thank you for your help.

tom us

4/9/2008 3:18:15 AM

Top54u

Hi Tom

Please try the free source code provided under this article.

Check the VB code of default.aspx page. You will notice a function name imageURL that takes 1 parameter as img_id. It passes the img_id to the retrieveImages.aspx page to retrieve the binary stream of image stored in Access Database.

Also see the HTML code of default.aspx page. There you will get the code for gridView having a template field with asp:Image control calling the above imageURL function to display the image.

Top54u us

7/23/2008 1:43:22 PM

James

This is a great piece of code.
I am trying to adapt it to work with a database I already have.
Unfortunatly the image is stored as a Memo rather than a OleObject. Might there be a simple way to use the display code you present here to display an image from an Access Database where the picture is in a memo field?
Thank you for your help.

James us

7/23/2008 5:35:40 PM

James

It does not appear that my last post saved. If I end up double posting, I apologize. This is a fantastic piece of code. I trying to adapt this code to use with a database i already have. Unfortunatly the image on my database is in an Access memo field which uses LongText rather than an OLEObject which uses LongBinary. Might you have any suggestion on how to display an image kept in a memo field?
Thank You for your help.
- James

James us

7/24/2008 6:16:41 AM

top54u.com

Hi James

You can learn the use of MS Access Memo field to store and display images in ASP.Net from the following article:

ASP.Net Upload Images to Ms Access Memo Field

URL: programming.top54u.com/.../...cess-Memo-Field.aspx

top54u.com us

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:42:18 AM




related videos.....
recent posts.....
top54u ezines.....