DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Asynchronous DataShow in Asp.Net 2.0 and C#

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#.

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>";
}

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!


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();
}
}

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.



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

Looking for more ASP.NET Tutorials? Click Here!
Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!