Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to retrieve image from Access database and display on Image control in ASP.NET

Author: Mike Green
Download Source Code :

I will explain in this article that how we can retrieve images from Access database and how we can display these retrieved images on Image control in ASP.NET.

In my previous article on DevASP.NET, I have explained how to save images in Access database. We can use a Generic Handler to retrieve images from any database including MS Access database but I have used different technique to retrieve images from Access and display it on Image control in ASP.NET. I have decided to save image on my disk and then I have set the path of the image on disk as URL of the Image control. I have used MemoryStream and Bitmap classes along with other classes for this purpose. The technique I am using might be not suitable for you then you can use Generic Handler technique. MS Access database I have used for this article is the same which I have used for my previous article of saving images to MS Access
  1. Open MS Visual Studio 2010
  2. File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
  3. Select Web Location as File System and Click OK
  4. From Menu, Website > Add New Item > Select Web Form and Click Add.
  5. Write below connection string in web.config file


        <add name="ImagesDB" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\ImagesDB.accdb"/>


  6. Write code below in Default.aspx page

    <asp:Button ID="btnRetrieveImage" runat="server"

                Text="Retrieve Image From Access" onclick="btnRetrieveImage_Click" />

    <br />

    <asp:Image ID="Image1" runat="server" />

  7.  Include following namespaces in your code file

    using System.Configuration;
    using System.Data.OleDb;
    using System.Data;
    using System.IO;
    using System.Drawing;
    Imports System.Configuration
    Imports System.Data.OleDb
    Imports System.Data
    Imports System.IO
    Imports System.Drawing

  8. Write below code in Button click event to retrieve image from MS Access Database

    protected void btnRetrieveImage_Click(object sender, EventArgs e)
        string connString = ConfigurationManager.ConnectionStrings["ImagesDB"].ConnectionString;
        OleDbConnection connection = new OleDbConnection(connString);
        string selectQuery = "SELECT ImageName, ImageData FROM Images WHERE ImageID = @ImageID";
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = selectQuery;
        command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("@ImageID", 3);
            OleDbDataReader reader = command.ExecuteReader();
            while (reader.Read())
                string imageName = reader[0].ToString();
                byte[] imageBytes = (byte[])reader[1];
                MemoryStream imageStream = new MemoryStream();
                imageStream.Write(imageBytes, 0, imageBytes.Length);
                Bitmap imageBitmap = new Bitmap(imageStream);
                Response.ContentType = "image/jpeg";
                imageBitmap.Save("D:\\Images\\" + imageName);
                Image1.ImageUrl = "D:\\Images\\" + imageName;
        catch (Exception ex)
    Protected Sub btnRetrieveImage_Click(sender As Object, e As System.EventArgs) Handles btnRetrieveImage.Click
        Dim connString As String = ConfigurationManager.ConnectionStrings("ImagesDB").ConnectionString
        Dim connection As New OleDbConnection(connString)
        Dim selectQuery As String = "SELECT ImageName, ImageData FROM Images WHERE ImageID = @ImageID"
        Dim command As New OleDbCommand()
        command.Connection = connection
        command.CommandText = selectQuery
        command.CommandType = CommandType.Text
        command.Parameters.AddWithValue("@ImageID", 3)
            Dim reader As OleDbDataReader = command.ExecuteReader()
            While reader.Read()
                Dim imageName As String = reader(0).ToString()
                Dim imageBytes As Byte() = DirectCast(reader(1), Byte())
                Dim imageStream As New MemoryStream()
                imageStream.Write(imageBytes, 0, imageBytes.Length)
                Dim imageBitmap As New Bitmap(imageStream)
                Response.ContentType = "image/jpeg"
                imageBitmap.Save("D:\Images\" + imageName)
                Image1.ImageUrl = "D:\Images\" + imageName
            End While
        Catch ex As Exception
        End Try
    End Sub
    First we have set our connection to MS Access database by getting connection string from web.config file. Then we have written a SELECT query to get image name and image binary data from table. I have provided the command text, command type and parameter to OleDbCommand object. I have provided the value of ImageID as 3 but you can provide this value according to your requirements. I have called the ExecuteReader() and created OleDbDataReader object. I have read the image name and bytes data from database and created an object of MemoryStream class. Then I have called the Write() method of MemoryStream object which will write block of bytes to current stream. I have created an object of BitMap class by providing the MemoryStream object. I have provided content type as “image/jpeg” but you can provide more content types according to your requirements. I have saved the image in disk and provide the path of the image as URL to Image control.
  9. Now you can browse the website and save image in MS Access Database


Add Article Comment:
Name :
Email Address :
Comments :
<< How to save images in MS Access database in ASP.NET

Disclaimer - Privacy
© 2002-2017