Pages

Wednesday, 8 January 2014

Image Retrieve from database using Generic Handler

Call from default.aspx


     string name = Session["name"].ToString();
            Image1.ImageUrl = "~/Handler.ashx?id=" + name;





Handler.ashx  file:

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

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ConnectionString);
        conn.Open();
        SqlCommand command = new SqlCommand("ImageSelect ", conn);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@imagername", SqlDbType.VarChar).Value = context.Request.QueryString["id"];

        SqlDataReader dr = command.ExecuteReader();
        if (dr.Read())
        {
            context.Response.ContentType = dr["images"].ToString();
            context.Response.BinaryWrite((byte[])dr["images"]);
        }
        else
        {
        }
        dr.Close();
        conn.Close();
       
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

No comments:

Post a Comment