Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,
I am displaying images in tempalte field of grid view with the help of http handler.but the problem is that it will show the image of the last item in all the columns of the grid view.
http handler class is following where i pass item id with the help of query string:
C#
using System;
using System.Configuration;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context)
    {
        Int32 itemid;
        if (context.Request.QueryString["id"] != null)
            itemid = Convert.ToInt32(context.Request.QueryString["id"]);
        else
            throw new ArgumentException("No parameter specified");

        context.Response.ContentType = "image/jpeg";
        Stream strm = ShowEmpImage(itemid);
        if (strm == null)
        {
            return;
        }
        byte[] buffer = new byte[4096];
        int byteSeq = strm.Read(buffer, 0, 4096);

        while (byteSeq > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, byteSeq);
            byteSeq = strm.Read(buffer, 0, 4096);
        }
        //context.Response.BinaryWrite(buffer);
    }
    public Stream ShowEmpImage(int itemid)
    {
        string conn = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
        SqlConnection connection = new SqlConnection(conn);
        string sql = "SELECT item_image FROM TbItem WHERE item_id = @ID";
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@ID", itemid);
        connection.Open();
        object img = cmd.ExecuteScalar();
        try
        {
            return new MemoryStream((byte[])img);
        }
        catch
        {
            return null;
        }
        finally
        {
            connection.Close();
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

I bind images at row data bound:

protected void gridhome_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            DataRowView r = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                CheckBox chk;
                chk = (CheckBox)(e.Row.FindControl("CheckBox3"));
                int id = Convert.ToInt32(DrpCat.SelectedValue);
                DataSet ds = objitem.fill_grid_show_by_items(id);
                string check = ds.Tables[0].Rows[0][1].ToString();
                if (check == "Checked")
                {
                    chk.Checked = true;
                }
                Image img;
                img = (Image)(e.Row.FindControl("imgPrettyPic"));
               
               
                int itemid = Convert.ToInt32(ds.Tables[0].Rows[0][3]);
                img.ImageUrl = "~/Handler1.ashx?id=" + itemid;
                img.DataBind();

               
              
            }
        }
        catch (Exception ex)
        {
        }
    }

but the problem is that it will return the image of the last id
the row data bound will fire the http handler at the last
Posted
Updated 18-Oct-10 23:17pm
v3
Comments
Dalek Dave 19-Oct-10 4:38am    
Edited for Code Block.

1 solution

C#
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{

   int itemid = Convert.ToInt32(ds.Tables[0].Rows[i][3]);
   img.ImageUrl = "~/Handler1.ashx?id=" + itemid;
   img.DataBind();
}


The reason is that you are looping through all the rows in a the dataset, and then setting the imageUrl to the last one every time. What you really need to ask yourself is Why are you using a loop if you only have 1 image on the row?
 
Share this answer
 
Comments
amit2620 19-Oct-10 5:17am    
but without loop it also show the last item ids image
Ryan Gamal 19-Oct-10 5:31am    
I can't see anywhere in your code where you are getting the id of the image, relevant to the row of data that you are databinding. Normally when writing an event handler for a RowDataBound event, you would use a property of e.Item.DataItem to identify the id of the image you wanted.

E.g. Product p = (Product)e.Item.DataItem;
image.ImageUrl = p.itemId;
amit2620 19-Oct-10 6:42am    
yah I got its answer
U were wrong i have to loop through to get image
for (int i = 0; i <= gridhome.Rows.Count; i++)
{
Image img;
img = (Image)(e.Row.FindControl("imgPrettyPic"));
int itemid = Convert.ToInt32(ds.Tables[0].Rows[i][3]);
img.ImageUrl = "~/Handler1.ashx?id=" + itemid;

}
Ryan Gamal 19-Oct-10 11:09am    
I still don't understand why you need a loop! You are setting the image url for every item in your loop, but it's the same image you are setting the url for every time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900