Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 21, 2012 11:30 PM by Bobby-Z
Member
3 Points
16 Posts
Dec 21, 2012 06:52 PM|LINK
Hi,
I have few images in my table which i am trying to bind in gridview and show it to user.But all otheer columns are getting dispalyed except the image.
here is my code.
==================================================================================
<asp:TemplateField HeaderText="Image" SortExpression="Image">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Image") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#"ImageHandler.ashx?id=" + Eval("ProductType") %>' Width="60" Height="60" />
</ItemTemplate>
</asp:TemplateField>
My ImageHandler.ashx code is
========================================
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace StudentManagement
{
/// <summary>
/// Summary description for ImageHandler
// </summary>
public class ImageHandler : IHttpHandler
byte[] empPic = null;
long seq = 0;
public void ProcessRequest(HttpContext context)
string product;
if (context.Request.QueryString["id"] != null)
product = context.Request.QueryString["id"];
else
throw new ArgumentException("No parameter specified");
context.Response.OutputStream.Write(ShowEmpImage(product), 78,Convert.ToInt32(seq) - 78);
}
public byte[] ShowEmpImage(string productno)
string conn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT Image FROM cartproducts WHERE ProductType = @productID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType =
CommandType.Text;
cmd.Parameters.AddWithValue(
"@productID", productno);
connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
seq = dr.GetBytes(0, 0,
null, 0, int.MaxValue) - 1;
empPic =
new byte[seq + 1];
dr.GetBytes(0, 0, empPic, 0,
Convert.ToInt32(seq));
connection.Close();
return empPic;
public bool IsReusable
get
return false;
the querystring input i am sendind it from another page as like
"Itemsdisplay.aspx?ProductType=Baby"
My table cartproducts has 5 columns out of which has two columns namely Producttype and image
according to the product type which comes from user i am trying to display the image whose logic is present in the httphandler page
the image type is varbinary in my table and the query which i wrote to insert image into databse manually is
as below
Insert into cartproducts (Pid, ProductName, ProductType,Image)
Select 1009, 'shoes','Baby', BulkColumn from Openrowset( Bulk 'F:\New folder\StudentManagement\StudentManagement\images\Baby\shoes.jpg', Single_Blob) as Image
But still then all others are getting reflected into gridview but not the image column
Please help
Thanks in advance
Contributor
2838 Points
1120 Posts
Dec 21, 2012 11:30 PM|LINK
how is this field set up? an image name, a url?
if it is an image then use this
<asp:image runat="server" ImageUrl='<%# Eval("Image", "www.site.com/image/{0}.jpg") %>
this is if you are trying to display an actual image, not an image name
so in this case try
Eval("ProductType", "ImageHandler.ashx?id={0}")
fanu
Member
3 Points
16 Posts
image not displayed in gridview
Dec 21, 2012 06:52 PM|LINK
Hi,
I have few images in my table which i am trying to bind in gridview and show it to user.But all otheer columns are getting dispalyed except the image.
here is my code.
==================================================================================
<asp:TemplateField HeaderText="Image" SortExpression="Image">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Image") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#"ImageHandler.ashx?id=" + Eval("ProductType") %>' Width="60" Height="60" />
</ItemTemplate>
</asp:TemplateField>
My ImageHandler.ashx code is
========================================
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
namespace StudentManagement
{
/// <summary>
/// Summary description for ImageHandler
// </summary>
public class ImageHandler : IHttpHandler
{
byte[] empPic = null;
long seq = 0;
public void ProcessRequest(HttpContext context)
{
string product;
if (context.Request.QueryString["id"] != null)
product = context.Request.QueryString["id"];
else
throw new ArgumentException("No parameter specified");
context.Response.OutputStream.Write(ShowEmpImage(product), 78,Convert.ToInt32(seq) - 78);
}
public byte[] ShowEmpImage(string productno)
{
string conn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(conn);
string sql = "SELECT Image FROM cartproducts WHERE ProductType = @productID";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType =
CommandType.Text;
cmd.Parameters.AddWithValue(
"@productID", productno);
connection.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
seq = dr.GetBytes(0, 0,
null, 0, int.MaxValue) - 1;
empPic =
new byte[seq + 1];
dr.GetBytes(0, 0, empPic, 0,
Convert.ToInt32(seq));
connection.Close();
}
return empPic;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
the querystring input i am sendind it from another page as like
"Itemsdisplay.aspx?ProductType=Baby"
My table cartproducts has 5 columns out of which has two columns namely Producttype and image
according to the product type which comes from user i am trying to display the image whose logic is present in the httphandler page
the image type is varbinary in my table and the query which i wrote to insert image into databse manually is
as below
Insert into cartproducts (Pid, ProductName, ProductType,Image)
Select 1009, 'shoes','Baby', BulkColumn from Openrowset( Bulk 'F:\New folder\StudentManagement\StudentManagement\images\Baby\shoes.jpg', Single_Blob) as Image
But still then all others are getting reflected into gridview but not the image column
Please help
Thanks in advance
Bobby-Z
Contributor
2838 Points
1120 Posts
Re: image not displayed in gridview
Dec 21, 2012 11:30 PM|LINK
how is this field set up? an image name, a url?
if it is an image then use this
<asp:image runat="server" ImageUrl='<%# Eval("Image", "www.site.com/image/{0}.jpg") %>
this is if you are trying to display an actual image, not an image name
so in this case try
Eval("ProductType", "ImageHandler.ashx?id={0}")
Currently Learning: ASP, SQL, CSS, JavaScript, AJAX, XML, XSLT, C# So please be patient with me! Thanks