top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

0 votes
1,147 views
How to retrieve image from Access database and display on Image control in ASP.NET?
posted Mar 7, 2016 by Sathaybama

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

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

<connectionStrings>

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

</connectionStrings>

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" />
  1. Include following namespaces in your code file

C#

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

VB.NET

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

C#

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);

    try
    {
        connection.Open();
        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)
    {
        Response.Write(ex.Message);
    }
    finally
    {
        connection.Close();
    }
}

VB.NET

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)

    Try
        connection.Open()
        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
        Response.Write(ex.Message)
    Finally
        connection.Close()
    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

answer Mar 8, 2016 by Shivaranjini
...