Click here to Skip to main content
15,895,423 members
Articles / Web Development / HTML

GridViewImages from DB in ASP.NET using C#

Rate me:
Please Sign up or sign in to vote.
4.80/5 (12 votes)
26 Jun 2009CPOL4 min read 82.4K   2.5K   45  
GridViewImages from DB in ASP.NET using C#
<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Data;
using System.Web;
using System.Data.SqlClient;
using System.IO;

public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True"); 
  myConnection.Open(); 
  string sql = "Select Image_Content from ImageGallery where Img_Id=@ImageId"; 
  SqlCommand cmd = new SqlCommand(sql, myConnection); 
  cmd.Parameters.Add("@ImageId",SqlDbType.Int).Value =Convert.ToInt32(context.Request.QueryString["id"]); 
  cmd.Prepare(); 
  SqlDataReader dr = cmd.ExecuteReader(); 
  dr.Read();
  context.Response.ContentType = "jpeg";//dr["Image_Type"].ToString(); 
  context.Response.BinaryWrite((byte[])dr["Image_Content"]); 

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer Pasca Software Solutions
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions