Display images in the data grid (ASP.NET PROGRAM)



          Uploading images to the database, retrieves images from database and displays it in the data grid. To display images we are using handler.ashx file
Products.aspx
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class products : System.Web.UI.Page
     {
        SqlConnection cn = new SqlConnection("Data Source=COMPUTER10; Initial Catalog=shoppingtrolly; Integrated Security=true");
    protected void Page_Load(object sender, EventArgs e)
    {
      
        cn.Open();
        SqlCommand cmd = new SqlCommand("select PID,PName,BName,MRP,Offp from productdetails", cn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet dt = new DataSet();
        da.Fill(dt);
        gdproduct.DataSource = dt;
        gdproduct.DataBind();
        cn.Close();
    }
    protected void btnsearch_Click(object sender, EventArgs e)
    {
    }
    protected void Item_Click(Object sender, DataGridCommandEventArgs e)
    {
        cn.Open();
       
        SqlCommand cmd1 = new SqlCommand("select PID,PName from productdetails", cn);
        SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
        DataSet ds1 = new DataSet();
        da1.Fill(ds1);
        if (((LinkButton)e.CommandSource).CommandName == "SelectItem")
        {
            string debug = "";
            try
            {
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    string key = gdproduct.DataKeys[e.Item.ItemIndex].ToString();
                    if (key == gdproduct.DataKeys[e.Item.ItemIndex].ToString())
                    {
                        lblm.Text = gdproduct.DataKeys[e.Item.ItemIndex].ToString();
                        Session["v"] = lblm.Text;
                        Response.Redirect("prdetails.aspx");
                    }
                }
            }
            catch (Exception exc)
            {
                debug = exc.Message;
            }
        }
    }
    protected void ResetPageIndex(DataGrid gdproduct, DataView view)
    {
       
        if ((gdproduct.CurrentPageIndex != 0) && (((gdproduct.CurrentPageIndex) * gdproduct.PageSize) >= view.Count))
        {
           
            if ((view.Count % gdproduct.PageSize) == 0)
            {
    gdproduct.CurrentPageIndex = (view.Count / gdproduct.PageSize) - 1;
            }
            else         
  {
       gdproduct.CurrentPageIndex = (view.Count / gdproduct.PageSize);
            }
        }



    }
}
ImageHandler.ashx
<%@ WebHandler Language="C#" Class="ImageHandler" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class ImageHandler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        //context.Response.ContentType = "text/plain";
        //context.Response.Write("Hello World");
        //int ImID = 2;
       string imageid=context.Request.QueryString["PrID"];
        SqlConnection cn=new SqlConnection("Data Source=COMPUTER10; Initial Catalog=shoppingtrolly; Integrated Security=true");
        cn.Open();
        SqlCommand cmd=new SqlCommand("select PImage from productdetails where PID="+imageid, cn);
        SqlDataReader dr=cmd.ExecuteReader();
        dr.Read();
        context.Response.BinaryWrite((Byte[])dr[0]);
        cn.Close();
        context.Response.End();
    }

    public bool IsReusable {
        get {
            return true;
        }
    }
}
HTML code
<asp:DataGrid ID="gdproduct" style="z-index:101; top:300px; left:50px;" Width="700px" runat="server" DataKeyField="PID" AutoGenerateColumns="False" OnItemCommand="Item_Click" BorderWidth="3px" BorderStyle="Inset" >        
<Columns>
<asp:TemplateColumn HeaderText="Image">
<ItemTemplate >
<asp:ImageButton ID="imgbtn" runat="server" ImageUrl='<%# "ImageHandler.ashx?PrID="+Eval("PID") %>' />
</ItemTemplate>
    <HeaderStyle ForeColor="Chocolate" Font-Bold="true" Font-Size="X-Large" />
</asp:TemplateColumn>
<asp:BoundColumn HeaderText = "ProductID" DataField="PID">
    <HeaderStyle ForeColor="Chocolate" Font-Bold="true" Font-Size="X-Large" />
</asp:BoundColumn>
<asp:BoundColumn DataField="PName" HeaderText="ProductName">
    <HeaderStyle ForeColor="Chocolate" Font-Bold="true" Font-Size="X-Large" />
</asp:BoundColumn>
<asp:BoundColumn DataField="BName" HeaderText="BrandName">
    <HeaderStyle ForeColor="Chocolate" Font-Bold="true" Font-Size="X-Large" />
</asp:BoundColumn>
<asp:BoundColumn DataField="MRP" HeaderText="MaximumPrice">
    <HeaderStyle ForeColor="Chocolate" Font-Bold="true" Font-Size="X-Large" />
</asp:BoundColumn>
<asp:BoundColumn DataField="Offp" HeaderText="OfferPrice">
    <HeaderStyle ForeColor="Chocolate" Font-Bold="true" Font-Size="X-Large" />
</asp:BoundColumn>
<asp:ButtonColumn Text="MoreDetail" HeaderText="Moredetail" CommandName="SelectItem">
    <HeaderStyle ForeColor="Chocolate" Font-Bold="true" Font-Size="X-Large" />
</asp:ButtonColumn>
</Columns>
    <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
        Font-Underline="False" />
</asp:DataGrid>

OUTPUT
Share:

Total Pageviews

CONTACT US

Prof. Roshan P. Helonde
Mobile: +917276355704
WhatsApp: +917276355704
Email: roshanphelonde@rediffmail.com

Enter Project Title

Popular Projects

All Archive

Contact Form

Name

Email *

Message *