Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
there is  one handler
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.IO;
using System.Data.SqlClient;

C#
public class Handler : IHttpHandler 
{
   public string GetConnectionString()
    {
        return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
    }
    public void ProcessRequest (HttpContext context)
    {
        string id = context.Request.QueryString["id"]; 
        if (id != null)
        {    MemoryStream memoryStream = new MemoryStream();
            SqlConnection connection = new SqlConnection(GetConnectionString());
            string sql = "SELECT * FROM TblImages WHERE Id = @id";
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.Parameters.AddWithValue("@id", id);
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            //Get Image Data
            byte[] file = (byte[])reader["Image"];
            reader.Close();
            connection.Close();
            memoryStream.Write(file, 0, file.Length);
            context.Response.Buffer = true;
            context.Response.BinaryWrite(file);
            memoryStream.Dispose();
        }}
     public bool IsReusable {
        get {
            return false;    }   }}

and there is form with dropdownlist with values and there is event
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
          Image1.ImageUrl = "Handler.ashx?id=" + DropDownList1.SelectedItem.Value;                       GetImageInfo(DropDownList1.SelectedItem.Value);
       }

application is running with no error but i have one question that howcome in "id"(in handler code)value gets 1,2 or 3 (as per dropdownlist items) as qhen i run the url on browswer have only http://localhost:60686/server_controls/2.%20image_retrieving_database.aspx" then how it gets value fetched in context.Request.QueryString["id"];
pls tell regards
Posted
Comments
Devang Vaja 19-Feb-13 7:49am    
hey in selected index change You are calling handler in image url...
when handler is called the querystring is not passed but only request goes...
querystring is passed when request is go from 1 page to another and querystring is always passed in response.redirect("?a="++"); statement
Shivani ji....

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900