Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Images in GridView

Status
Not open for further replies.

cpope

Programmer
Jul 7, 2000
58
US
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:parameter Name="CategoryId" Type="Int32" />
<asp:parameter Name="ItemNumber" Type="String" />
<asp:parameter Name="ItemText" Type="String" />
<asp:parameter Name="HtmlImageWidth" Type="Int16" />
<asp:parameter Name="HtmlImageHeight" Type="Int16" />
<asp:parameter Name="Image" Type="Object" />
<asp:parameter 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!!!
 
cpope: Just a shot in the dark but looking at one of my DataGrids (caputring images) I have:
Code:
<asp:TemplateColumn>
  <ItemTemplate>   
    <asp:Image id="imgInsect" runat="Server" ImageUrl=<%#Container.DataItem("ImagePath")%>/>
  </ItemTemplate>  
</asp:TemplateColumn>
..so perhaps leaving out runat="server" might be an issue.
 
Thanks for the reply,
unfortunately that didn't work either :(
... any other ideas?
 
The reason was due to the fact that my column name was a reserved word "Image", I also created an object datasource for me to manually insert the data, I am guessing the Object asp:parameter did not jive... Argh!! ah well. It's working now!
 
cpope: Glad you found it. You do have to be careful about naming conventions - thanks for posting back.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top