Microsoft KB Archive/318131

= HOW TO: Page Through a Query Result for Better Performance =

Article ID: 318131

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1

-



This article was previously published under Q318131



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * Methods to Implement Paging
 * Custom Paging Sample



SUMMARY
This step-by-step article provides guidelines about how to implement paging. This article provides a few examples of paging and describes the advantage of each method.

back to the top

Methods to Implement Paging
There are many approaches to paging. Paging is typically an architectural issue. Paging largely depends on your database design and how many records you have to page. You can use either of the following methods to implement paging:  Use the intrinsic paging that is available in the DataGrid control. This method of paging is easy to use and works well when you have a small number of records to page. However, performance decreases when the number of records increase. In Microsoft Visual Basic .NET, there is no intrinsic paging.For additional information about how to implement paging, click the article number below to view the article in the Microsoft Knowledge Base:

305271 HOW TO: Custom Page a DataGrid Windows Control by Using Visual Basic .NET

 To speed up performance, design queries or stored procedures to retrieve the number of records that you want. The Custom Paging Sample section demonstrates how to implement custom paging.

You can also use the TOP and the WHERE clauses in your SQL statement to design queries or stored procedures to retrieve the number of records that you want. For more information about how to use TOP and WHERE to implement custom paging, visit the following Microsoft Developer Network (MSDN) Web site:

Paging Through a Query Result

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconpagingthroughqueryresult.asp



back to the top

Custom Paging Sample
This sample demonstrates how to use a DataGrid control to implement custom paging through ASP.NET. When you use this method, you can retrieve the records that you want in any page order. As a result, this method is highly efficient and versatile.

This sample requires that the database include tables that have an index. Although this sample uses Microsoft SQL Server and the SQL Server Northwind database, you can adapt this sample to use with any database.  Follow these steps to create a new Visual Basic ASP.NET Web Application:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the New Project dialog box, click Visual Basic Projects under Project Types, and then click ASP.NET Web Application under Templates.</li></ol> </li> Drag a DataGrid control from the toolbox to WebForm1.aspx.</li> Right-click DataGrid1, and then click Property Builder.</li> Follow these steps in the Property Builder dialog box: <ol style="list-style-type: lower-alpha;"> Click Paging.</li> Click to select the AllowPaging and the AllowCustomPaging check boxes.</li> Set the PageSize property to 3 .</li> In the Mode list, click Page Numbers.</li> Click Apply, and then click OK.</li></ol> </li>  Click HTML at the bottom of WebForm1.aspx to open the HTML source view. Replace the <asp:DataGrid> tag that is added by default with the following code: <asp:DataGrid id=&quot;Datagrid1&quot; runat=&quot;server&quot; AllowCustomPaging=&quot;True&quot; PageSize=&quot;3&quot; AllowPaging=&quot;True&quot; PagerStyle-Mode=&quot;NumericPages&quot; OnPageIndexChanged=&quot;DataGrid1_PageIndexChanged&quot;> </asp:DataGrid> </li>  Double-click in an empty area of the WebForm1.aspx page to open the Code window, and then add the following namespaces to the top of the page: Imports System.Data Imports System.Data.SqlClient </li>  Add the following declaration under Public Class Webform1: Dim cn As SqlConnection Dim StartIndex As Integer Dim EndIndex As Integer </li>  Add the following code in the Page_Load event of the form: Dim myCommand As SqlCommand cn = New SqlConnection(&quot;server = localhost; uid=userid; pwd=password; database=Northwind &quot;) If Not IsPostBack Then myCommand = New SqlCommand myCommand.CommandText = &quot;Select Count(*) from Products&quot; myCommand.Connection = cn           cn.Open DataGrid1.VirtualItemCount = myCommand.ExecuteScalar cn.Close BindDatagrid End If                   </li>  Add the following code after the Page_Load subroutine: Sub BindDatagrid Dim myAdapter As SqlDataAdapter Dim DS As DataSet EndIndex = StartIndex + DataGrid1.PageSize myAdapter = New SqlDataAdapter(&quot;Select * From Products Where ProductID > @startIndex And ProductID <= @endIndex Order by ProductID&quot;, cn) myAdapter.SelectCommand.Parameters.Add(&quot;@startIndex&quot;, StartIndex) myAdapter.SelectCommand.Parameters.Add(&quot;@endIndex&quot;, EndIndex) DS = New DataSet myAdapter.Fill(DS) DataGrid1.DataSource = DS       DataGrid1.DataBind End Sub

Public Sub datagrid1_PageIndexChanged(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) StartIndex = (e.NewPageIndex * DataGrid1.PageSize) DataGrid1.CurrentPageIndex = e.NewPageIndex BindDatagrid End Sub </li> Press F5 to run the application.</li> To test the project, select any page number that is listed below the grid.</li></ol>

back to the top

Keywords: kbhowtomaster kbsqlclient kbsystemdata KB318131

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.