Tuesday, 26 July 2011

Asynchronous DataShow in Asp.Net 2.0 and VB.NET


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 VB.NET.

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.

Imports System.Data.SqlClient

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

Custom function BeginAsyncOperation ,EndAsyncOperation and Page_PreRenderComplete

Function BeginAsyncOperation(ByVal sender As Object, ByVal e As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object) As IAsyncResult
Dim connectionstring As String = "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)
End Function 'BeginAsyncOperation

Sub EndAsyncOperation(ByVal ar As IAsyncResult)
_reader = _command.EndExecuteReader(ar)
End Sub 'EndAsyncOperation

Private Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As EventArgs)

While _reader.Read()
_reader.Read()
Me.Label1.Text = Me.Label1.Text & _reader.GetValue(1) + "<br>"
End While
End Sub 'Page_PreRenderComplete

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.

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>

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The whole code behind front page:

Imports System.Data.SqlClient
Imports System.Web.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Private _connection As SqlConnection
Private _command As SqlCommand
Private _reader As SqlDataReader
Function BeginAsyncOperation(ByVal sender As Object, ByVal e As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object) As IAsyncResult
Dim connectionstring As String = "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)
End Function 'BeginAsyncOperation

Sub EndAsyncOperation(ByVal ar As IAsyncResult)
_reader = _command.EndExecuteReader(ar)

End Sub 'EndAsyncOperation

Private Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As EventArgs)
While _reader.Read()
_reader.Read()
Me.Label1.Text = Me.Label1.Text & _reader.GetValue(1) + "<br>"
End While
End Sub 'Page_PreRenderComplete
Public Overrides Sub Dispose()
If Not (_connection Is Nothing) Then
_connection.Close()
End If
MyBase.Dispose()
End Sub 'Dispose

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
AddHandler Me.PreRenderComplete, New EventHandler(AddressOf Page_PreRenderComplete)
Page.AddOnPreRenderCompleteAsync(New BeginEventHandler(AddressOf BeginAsyncOperation), New EndEventHandler(AddressOf EndAsyncOperation))
End If
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!


Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment