Hi all, I am currently trying to retrieve image data from an MS Access database and display it in a GridView. I am not sure exactly where I am going wrong, whether it be on the storing of the data (data entry via a DetailsView) or the display of the data, and was wondering if you could help me out. Here is some sample code:
STORING THE DATA
AccessDataSource Insert Command:
InsertCommand="INSERT INTO `Item` (`CategoryId`, `ItemNumber`, `ItemText`, `HtmlImageWidth`, `HtmlImageHeight`,`Image`,`Price`) VALUES (?, ?, ?, ?, ?,?,?)"
Insert Parameters for above:
<InsertParameters>
<asp
arameter Name="CategoryId" Type="Int32" />
<asp
arameter Name="ItemNumber" Type="String" />
<asp
arameter Name="ItemText" Type="String" />
<asp
arameter Name="HtmlImageWidth" Type="Int16" />
<asp
arameter Name="HtmlImageHeight" Type="Int16" />
<asp
arameter Name="Image" Type="Object" />
<asp
arameter Name="Price" Type="String" />
</InsertParameters>
DetailsView event
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
FileUpload loader = (FileUpload)((DetailsView)sender).FindControl("ImageUpload");
if (loader.HasFile)
{
Stream inputStream = loader.PostedFile.InputStream;
int imgLen = loader.PostedFile.ContentLength;
byte[] imageBin = new Byte
;
int inputRead = inputStream.Read(imageBin, 0, imgLen);
byte[] imageData = imageBin;
StringBuilder result = new StringBuilder();
for (int i = 0; i < imageData.Length; i++)
{
result.Append(imageData);
}
AccessDataSource2.InsertParameters["Image"].DefaultValue = result.ToString();
}
}
RETRIEVING THE IMAGE
public static byte[] GetImage(Int32 id)
{
string sql = "select image from Item where ItemId=?";
OleDbCommand cmd = new OleDbCommand(sql);
cmd.Parameters.Add(new OleDbParameter("ItemId", id));
return ExecuteScalarByte(cmd);
}
public static byte[] ExecuteScalarByte(OleDbCommand cmd)
{
OleDbConnection conn = new OleDbConnection(GetConnectionString());
cmd.Connection = conn;
object result = null;
try
{
conn.Open();
result = cmd.ExecuteScalar();
return (byte[])result;
}
catch (Exception)
{ return null; }
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
DISPLAYING THE IMAGE (IN GRIDVIEW):
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<img src='../ImageHandler.ashx?ItemId=<%# Eval("ItemId") %>' alt="balloon"/>
</ItemTemplate>
</asp:TemplateField>
HANDLER:
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using System.IO;
public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// Set up the response settings
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
Int32 ImageId = Convert.ToInt32(context.Request["ItemId"].ToString());
byte[] img = Helper.GetImage(ImageId);
context.Response.BinaryWrite(img);
}
public bool IsReusable
{
get {
return false;
}
}
}
This is really starting to frustrate me so any help would be enormously appreciated!!!
STORING THE DATA
AccessDataSource Insert Command:
InsertCommand="INSERT INTO `Item` (`CategoryId`, `ItemNumber`, `ItemText`, `HtmlImageWidth`, `HtmlImageHeight`,`Image`,`Price`) VALUES (?, ?, ?, ?, ?,?,?)"
Insert Parameters for above:
<InsertParameters>
<asp
<asp
<asp
<asp
<asp
<asp
<asp
</InsertParameters>
DetailsView event
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
FileUpload loader = (FileUpload)((DetailsView)sender).FindControl("ImageUpload");
if (loader.HasFile)
{
Stream inputStream = loader.PostedFile.InputStream;
int imgLen = loader.PostedFile.ContentLength;
byte[] imageBin = new Byte
int inputRead = inputStream.Read(imageBin, 0, imgLen);
byte[] imageData = imageBin;
StringBuilder result = new StringBuilder();
for (int i = 0; i < imageData.Length; i++)
{
result.Append(imageData);
}
AccessDataSource2.InsertParameters["Image"].DefaultValue = result.ToString();
}
}
RETRIEVING THE IMAGE
public static byte[] GetImage(Int32 id)
{
string sql = "select image from Item where ItemId=?";
OleDbCommand cmd = new OleDbCommand(sql);
cmd.Parameters.Add(new OleDbParameter("ItemId", id));
return ExecuteScalarByte(cmd);
}
public static byte[] ExecuteScalarByte(OleDbCommand cmd)
{
OleDbConnection conn = new OleDbConnection(GetConnectionString());
cmd.Connection = conn;
object result = null;
try
{
conn.Open();
result = cmd.ExecuteScalar();
return (byte[])result;
}
catch (Exception)
{ return null; }
finally
{
if (conn.State != ConnectionState.Closed)
conn.Close();
}
}
DISPLAYING THE IMAGE (IN GRIDVIEW):
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<img src='../ImageHandler.ashx?ItemId=<%# Eval("ItemId") %>' alt="balloon"/>
</ItemTemplate>
</asp:TemplateField>
HANDLER:
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using System.IO;
public class ImageHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// Set up the response settings
context.Response.ContentType = "image/jpeg";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
Int32 ImageId = Convert.ToInt32(context.Request["ItemId"].ToString());
byte[] img = Helper.GetImage(ImageId);
context.Response.BinaryWrite(img);
}
public bool IsReusable
{
get {
return false;
}
}
}
This is really starting to frustrate me so any help would be enormously appreciated!!!