Wednesday, 7 December 2011

Showing Image from database using Handler


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

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

public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context)
    {
        string query = "Select Image from  Table_Image WHERE Name = '" + context.Request.QueryString["Name"].ToString() +"'" ;
        SqlConnection connection = new SqlConnection("Type your Connection String Here");
        SqlCommand command = new SqlCommand(query, connection );
        connection .Open();
       
        byte[] img = (byte[])command.ExecuteScalar();
        context.Response.BinaryWrite(img);      
        connection.Close();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

After making a handler as above. Set Image URL like this ::


Image1.ImageUrl = "Handler.ashx?Name=" + TextBox1.Text;

No comments:

Post a Comment