Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Performance - Asynchronous DataShow in Asp.Net 2.0 and C#
Asynchronous DataShow in Asp.Net 2.0 and C#


ASP.NET Performance Tutorial

Asynchronous DataShow in Asp.Net 2.0 will improve capability of the page. This tutorial will show you how to create Asynchronous DataShow in ASP.Net 2.0 and C#.

Download the Full Working Version of this Project written with Visual Studio.NET C#.NET 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

First we should add Async="true" in the <%page > code.Then we use function Page. AddOnPreRenderCompleteAsync to register a function begin and function end.we use label control to show the title of table titles.

using System.Data.SqlClient

Custom function BeginAsyncOperation ,EndAsyncOperation and Page_PreRenderComplete

IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
{
string connectionstring = "server=localhost;uid=sa;pwd=1234;database=Pubs;Asynchronous Processing=true;";

_connection = new SqlConnection(connectionstring);
_connection.Open();

_command = new SqlCommand("select title_id,title,price from titles", _connection);
return _command.BeginExecuteReader(cb, state);
}
void EndAsyncOperation(IAsyncResult ar)
{
_reader = _command.EndExecuteReader(ar);
}
private void Page_PreRenderComplete(object sender, EventArgs e)
{

while (_reader.Read())
this.Label1.Text = this.Label1.Text + _reader.GetValue(1) + "<br>";
}

The front page of Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Default</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<fieldset><legend>AsyncDataBind</legend>&nbsp;&nbsp;
<asp:Label ID="Label1" runat="server" Text="Title: <br>"></asp:Label>
</fieldset>
</div>
</form>
</body>
</html>

The whole code behind front page:

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;
using System.Web.Configuration;

public partial class _Default : System.Web.UI.Page
{
private SqlConnection _connection;
private SqlCommand _command;
private SqlDataReader _reader;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
Page.AddOnPreRenderCompleteAsync(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation));
}
}
IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
{
string connectionstring = "server=localhost;uid=sa;pwd=1234;database=Pubs;Asynchronous Processing=true;";

_connection = new SqlConnection(connectionstring);
_connection.Open();

_command = new SqlCommand("select title_id,title,price from titles", _connection);
return _command.BeginExecuteReader(cb, state);
}
void EndAsyncOperation(IAsyncResult ar)
{
_reader = _command.EndExecuteReader(ar);
}
private void Page_PreRenderComplete(object sender, EventArgs e)
{

while (_reader.Read())
this.Label1.Text = this.Label1.Text + _reader.GetValue(1) + "<br>";
}
public override void Dispose()
{
if (_connection != null)
_connection.Close();
base.Dispose();
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C#.NET 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!






 
  Developer Resources







Server Intellect Rocks