Step : 2 Codes
Class : AsyncClass1.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;
/// <summary>
/// Summary description for AsyncClass1
/// </summary>
public class AsyncClass1
{
public AsyncClass1()
{
//
// TODO: Add constructor logic here
//
}
private static readonly string strCnn1;
private SqlCommand sqlCmd;
ProductList plist = new ProductList();
public IAsyncResult GetProduct(AsyncCallback callback, Object state)
{
SqlConnection con = new SqlConnection(strCnn1);
sqlCmd = new SqlCommand("SELECT * FROM ProductInfo", con);
con.Open();
return sqlCmd.BeginExecuteReader(callback, state,
CommandBehavior.CloseConnection);
}
public List<ProductList> EndProduct(IAsyncResult result)
{ List<ProductList> results = new List<ProductList>();
SqlDataReader reader = sqlCmd.EndExecuteReader(result);
while (reader.Read())
{
ProductList newProduct = new ProductList();
newProduct.ID = Convert.ToInt32(reader[plist.ID]);
newProduct.ProductName = Convert.ToString(reader["ProductName"]);
newProduct.ProducerName = Convert.ToString(reader["ProducerName"]);
newProduct.Price = Convert.ToDouble(reader["Price"]);
newProduct.Country =(string)reader["Country"];
results.Add(newProduct);
}
return results;
}
static AsyncClass1()
{
strCnn1 = WebConfigurationManager.ConnectionStrings["ProductCnn1"].ConnectionString
+ ";Asynchronous Processing=true";
}
//Inner class
}
Class: ProductList.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ProductList
/// </summary>
public class ProductList
{
public ProductList()
{
//
// TODO: Add constructor logic here
//
}
private int id;
private string productname;
private string producername;
private double price;
private string country;
public int ID
{
get { return id; }
set { id = value; }
}
public string ProductName
{
get { return productname; }
set { productname= value; }
}
public string ProducerName
{
get {return producername;}
set { producername = value; }
}
public double Price
{
get { return price; }
set { price = value; }
}
public string Country
{
get { return country; }
set { country = value; }
}
}
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
public partial class _Default : System.Web.UI.Page
{
private AsyncClass1 dataLayer = new AsyncClass1();
protected void Page_Load(object sender, EventArgs e)
{
// Setup asynchronous data execution
PageAsyncTask task = new PageAsyncTask(BeginGetData, TraceGetData,
TimeoutData, null, true);
Page.RegisterAsyncTask(task);
// Fire off asynchronous tasks
Page.ExecuteRegisteredAsyncTasks();
}
protected IAsyncResult BeginGetData(object sender, EventArgs e,
AsyncCallback callback, object state)
{
// Show Page Thread ID
L1.Text += "<hr/>";
Trace.Warn("BeginGetData: " + Thread.CurrentThread.GetHashCode());
// Execute asynchronous command
return dataLayer.GetProduct(callback, state);
}
protected void TraceGetData(IAsyncResult ar)
{
// Show Page Thread ID
Trace.Warn("Tracing Get Data : " + Thread.CurrentThread.GetHashCode());
// Bind results
GridView1.DataSource = dataLayer.EndProduct(ar);
GridView1.DataBind();
L1.Text += "<hr/>";
}
protected void TimeoutData(IAsyncResult ar)
{
// Display error message
L1.Text = "Could not retrieve data!";
}
}
Default.aspx.
<%@ Page Language="C#" AutoEventWireup="true" Async="true"
CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true"
Trace="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductCnn1 %>"
SelectCommand="SELECT * FROM [ProductInfo]"></asp:SqlDataSource>
<asp:Label ID="L1" runat="server" Text="Label"></asp:Label>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<br />
</div>
</form>
</body>
</html>
Web.cofig
<connectionStrings>
<add name="ProductCnn2" connectionString="Data Source=MANAS6\SQLEXPRESS;Initial
Catalog=PageAsync1;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="ProductCnn1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PageAsync1.mdf;Integrated
Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<identity impersonate="true" userName="Administrator"
password="Manas6"/>