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 VB.NET
Asynchronous DataShow in Asp.Net 2.0 and VB.NET


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

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

Looking for the C#.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.

Imports System.Data.SqlClient

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

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:

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

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

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

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks