How to store and retrieve images from Access database using ASP.NET?


In this article I will explain about how to store and retrieve image from access database using ASP.NET. Images are stored in the database as bytes.

Description

In my previous articles I have explained in detail about How to store image in SQL SERVER database table? and How to retrieve images from SQL SERVER database table etc. I have recently saw one more question in forum its related same like my previous article topics but user want to store images in the MS Access database.

First I have create table like below to store images

StrImageAccessDB


For example,

I have use file upload control to get user selected images and convert that image in to bytes then stored in the MS ACCESS database using like below code.

Insert image into MS ACCESS Database table


protected void Button1_Click(object sender, EventArgs e)
{
//Declare variable to keep file name
string fname = string.Empty;

//Declare one variable to assign image bytes after convert
byte[] FileBytes = null;

//First save the image in the server path to get full path
if (FileUpload1.HasFile)
{
fname = FileUpload1.FileName;
Stream fs = default(Stream);
fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
FileBytes = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
fs.Close();
fs.Dispose();
br.Close();
}
else
{
lblmsg.Text = "please select file";
return;
}

con.Open();
cmd = new OleDbCommand("insert into imgupload(ImgName,Img) values (@Im, @Img)", con);
cmd.Parameters.Add("@Im", fname);
cmd.Parameters.Add("@Img", FileBytes);
cmd.ExecuteNonQuery();
con.Close();
lblmsg.Text = "Image insert succcessfully";
}


Retrieve image from MS ACCESS Database table

Last inserted image show in image control using below code


protected void Button2_Click(object sender, EventArgs e)
{
con.Open();
//select last insert image using max (id)
cmd = new OleDbCommand("select * from imgupload where ID=(select max(ID) from imgupload)", con);
da = new OleDbDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["Img"] != DBNull.Value)
{
Session["dt"] = dt;
Image1.ImageUrl = "Default2.aspx";
}
}
con.Close();
}


Full Source Code

I have design page in the page like below

Client Side: Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Store and retrieve images from access database</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblmsg" runat="server"></asp:Label><br />
select image to upload into the access database<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Insert Image into Database" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Retrieve image from database and display"
OnClick="Button2_Click" /><br />
<br />
<asp:Image ID="Image1" runat="server" />
</div>
</form>
</body>
</html>

Code Behind: Default.aspx.cs


using System.Data;
using System.Data.OleDb;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/Database1.accdb") + ";");
OleDbCommand cmd;
OleDbDataAdapter da;
DataTable dt = new DataTable();

protected void Page_Load(object sender, EventArgs e)
{
lblmsg.Text = "";
}
//1) Insert image into Table as bytes
protected void Button1_Click(object sender, EventArgs e)
{
//Declare variable to keep file name
string fname = string.Empty;

//Declare one variable to assign image bytes after convert
byte[] FileBytes = null;

//First save the image in the server path to get full path
if (FileUpload1.HasFile)
{
fname = FileUpload1.FileName;
Stream fs = default(Stream);
fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
FileBytes = br.ReadBytes(FileUpload1.PostedFile.ContentLength);
fs.Close();
fs.Dispose();
br.Close();
}
else
{
lblmsg.Text = "please select file";
return;
}

con.Open();
cmd = new OleDbCommand("insert into imgupload(ImgName,Img) values (@Im, @Img)", con);
cmd.Parameters.Add("@Im", fname);
cmd.Parameters.Add("@Img", FileBytes);
cmd.ExecuteNonQuery();
con.Close();
lblmsg.Text = "Image insert succcessfully";
}

//2) Retrieve image from Table convert that bytes to images and display in the image control
protected void Button2_Click(object sender, EventArgs e)
{
con.Open();
//select last insert image using max (id)
cmd = new OleDbCommand("select * from imgupload where ID=(select max(ID) from imgupload)", con);
da = new OleDbDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if (dt.Rows[0]["Img"] != DBNull.Value)
{
Session["dt"] = dt;
Image1.ImageUrl = "Default2.aspx";
}
}
con.Close();
}
}


Code Behind: Default2.aspx.cs


using System.Data;
public partial class Default2 : System.Web.UI.Page
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
byte[] b = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["dt"] != null)
{
dt=(DataTable) Session["dt"];
if (dt.Rows.Count > 0)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
b = ((byte[])dt.Rows[0]["Img"]);
//Response.ContentType = "image/jpeg";
Response.BinaryWrite(b);
}
}
}
}
}


Output

The output of the above code is look like below image
StrImageAccessDB


Source Code

Here I have attached full source code for the same download it and test it.

Client Side : ASP.NET
Code Behind : C#

Conclusion

I hope this article is help you to know about how to store and retrieve image from MS ACCESS database.


Attachments

  • strImgAccess (44433-05936-strImgAccess.rar)
  • Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: