Tuesday, 26 July 2011

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

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

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

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

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>

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

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

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

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment