SIGN UP MEMBER LOGIN:    
ARTICLE

Display Images in the Grid View Control

Posted by Jaiganesh Articles | Windows Forms C# February 17, 2010
In this article you will learn how to Display Images in the Grid view control.
Reader Level:

Steps to display images in the grid view control:

  1. Create a table schema in the following format in Sql Server

    DataGrid1.gif
     
  2. Design the web form with the following controls

    Textbox, Fileupload control, Button, Gridview, Sqldatasource

    DataGrid2.gif
     
  3. Create the sqldatasource connectionstring with the datasource property and set the datakey name to ID
     
  4. Add a Generic handler to the application(In the solution explorer right click and add select generic handler from the Templates in the New Item

It consists the code of Selecting the records from the table and displaying them in the gridview

Handler code:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        // Create SQL Command

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Select ImageName,Image from Images where ID =@ID";
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;

        SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
        ImageID.Value = context.Request.QueryString["ID"];
        cmd.Parameters.Add(ImageID);
        con.Open();
        SqlDataReader dReader = cmd.ExecuteReader();
        dReader.Read();
        context.Response.BinaryWrite((byte[])dReader["Image"]);
        dReader.Close();
        con.Close();
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor