By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,314 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,314 IT Pros & Developers. It's quick & easy.

Asynchronous Problem in web application

P: n/a

I have a 2.0 web application.
I want to implement the asynchronous model through http handler in


My web.config file:
<?xml version="1.0"?>
<add name="AdvWorks" connectionString="Data Source=(local); Integrated
Security=SSPI; Initial Catalog=AdventureWorks; Asynchronous
<add verb="*" path="*.myasync" type="ProductAsyncHandler"/>
<compilation debug="true"/>


ProductAsyncHandler class
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

public class ProductAsyncHandler : IHttpAsyncHandler
#region IHttpHandler-related members (if you want to perform a synchronous
operation in the HTTP handler)

public void ProcessRequest(HttpContext context)
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");

public bool IsReusable
return false;


#region IHttpAsyncHandler-related members (if you want to perform an
asynchronous operation in the HTTP handler)

// The command object that will be performed asynchronously.
private SqlCommand _command;

// The HttpContext object that provides access to HTTP-related resources
(such as HTTP request and response objects).
private HttpContext _context;

/// <summary>
/// ASP.NET calls this method, to begin an asynchronous operation.
/// This method starts an asynchronous data access operation, and then
returns immediately to ASP.NET.
/// </summary>
/// <param name="context">Provides access to HTTP resources such as the
HTTP request and response</param>
/// <param name="cb">References the callback method to be called when the
asynchronous operation is complete.</param>
/// <param name="extraData">Additional state that will be passed into the
callnback method</param>
/// <returns>Represents the status of the asynchronous operation.</returns>
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback
cb, object extraData)
// Save the context parameter in an instance variable, so we can use it in
the callback method.
_context = context;

// Set up the method return variable.
IAsyncResult result = null;

// Create a connection object.
string connectionString =
SqlConnection connection = new SqlConnection(connectionString);

// Set up the command object, using the list price specified in the query
decimal listPrice = decimal.Parse(context.Request["ListPrice"]);
_command = new SqlCommand("uspGetProductsBelowListPrice", connection);
_command.CommandType = CommandType.StoredProcedure;
_command.Parameters.AddWithValue("@ListPrice", listPrice);

// Open the database connection.

// Perform the command asynchronously.
result = _command.BeginExecuteReader(cb, listPrice);
catch (Exception)
// Only close the connection if an exception occurs.
// Otherwise, keep it open for the asynchronous command.
// The callback method will close the connection when the command is

// Rethrow the exception, so ASP.NET can handle it.

// Return status information to ASP.NET.
return result;

/// <summary>
/// ASP.NET calls this method when the asynchronous data-access command is
/// This method retrieves the result of the command, and then closes the
/// </summary>
/// <param name="result">Represents the status of the asynchronous
public void EndProcessRequest(IAsyncResult result)
// Get the data reader returned by the query.
SqlDataReader reader = _command.EndExecuteReader(result);

// Iterate through the product rows returned by the query.
_context.Response.Write("<h1>Products that cost less than $" +
result.AsyncState.ToString() + "</h1>");
while (reader.Read())
string productDetails = String.Format("[{0}] {1}, ${2}<br/>",

// Close the connection and wrap up.
_command = null;



Default Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>


<html xmlns="" >
<head runat="server">
<title>Asynchronous Callback Test</title>
<form id="form1" runat="server">

<a href="anything.myasync?ListPrice=1000">Get Products that cost
less than $1000</a>
<br />
<a href="anything.myasync?ListPrice=2000">Get Products that cost
less than $2000</a>
<br />
<a href="anything.myasync?ListPrice=3000">Get Products that cost
less than $3000</a>
<br />

No Code Behind

Thank you.
Oct 26 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.