asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Paging: Use ADO, getrows, or a Stored Procedure?
By Stephen Lian
Rating: 4.0 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Presenting a large set of data in readable pages is a standard task for Web- application developers. Applications that need to produce search results in a digestible manner need to apply paging algorithms. This article will compare the three ways in which paging can be achieved for an ASP/SQL Server 7 application. The first uses ActiveX Data Object (ADO) paging; the second uses getrows and an array; and the third uses a SQL Server 7 stored procedure. Of those, the third is the most efficient because it returns the minimum set of data from the database server to the Web server.

    Solution 1: Paging via ADO

    To use ADO's paging system, the cursor location of either the recordset or connection object must be set to adUseClient, or a client-side cursor, before the recordset is filled.

    
    rstProjects.CursorLocation = adUseClient
    
    
    Next, execute a command or query and obtain a recordset. Set the page size property of the recordset to the number of records that a page should show. And set the absolute page property of the recordset to the current page number. These two properties enable ADO to find and present the subset of the recordset that is relevant for the current page. Note that with these properties set, ADO will still return the entire recordset to the client; only the BOF and EOF markers (ADO properties) will be different.

    To maintain state information about the current page, either use a query string or a hidden field. In either case, ensure that if the user chooses to move to the next page, the current page variable increases by one, and if the user chooses to move to the previous page, the current page variable decreases by one.

    
    If (Len(Request.QueryString("page")) = 0) Or (IsNumeric(Request.QueryString("page")) = False) Then
    	intCurrentPage = 1
    Else
    	intCurrentPage = CInt(Request.QueryString("page"))
    End If
    rstProjects.PageSize = 25
    rstProjects.AbsolutePage = intCurrentPage
    
    
    The next step is to construct the loop that will display the records for the current page. As per sound ADO practice, when looping through a recordset to display data, set up local variables that reference the various fields and then access those local variables.
    
    If rstProjects.RecordCount > 0 Then
    
    Dim fldProject, fldBuyer, fldBidder, fldAverageBid
    Set fldProject = rstProjects.Fields("Project")
    Set fldBuyer = rstProjects.Fields("Buyer")
    Set fldBidder = rstProjects.Fields("Bidder")
    Set fldAverageBid = rstProjects.Fields("AverageBid")
    
    Dim intCurrentRecord
    intCurrentRecord = 1
    
    Do
    	Response.Write("<p>" & fldProject.Value & "</p>" & vbCrLf)
    	Response.Write("<p>" & fldBuyer.Value & "</p>" & vbCrLf)
    	Response.Write("<p>" & fldBidder.Value & "</p>" & vbCrLf)
    	Response.Write("<p>" & fldAverageBid.Value & "</p>" & vbCrLf)
    
    	rstProjects.MoveNext
    	intCurrentRecord = intCurrentRecord + 1
    	If rstProjects.EOF Then Exit Do
    Loop While intCurrentRecord < rstProjects.PageSize
    
    
    Any kind of loop could achieve the same effect, but I've presented a fairly verbose Do-Loop-While loop for heuristic purposes.

    Solution 2: Paging via getrows and an Array

    The getrows approach will mimic the same algorithm as ADO paging but will use an array instead of an ADO recordset. By putting all the data into an array shortly after the recordset is filled, the recordset object can be closed and set to nothing, thereby freeing up system resources. As a result, this approach should be more efficient that the ADO approach.

    
    If rstProjects.RecordCount > 0 Then
    
    Dim arrProjects
    arrProjects = rstProjects.GetRows
    
    rstProjects.Close
    Set rstProjects = Nothing
    
    Dim intTotalRecords
    intTotalRecords = UBound(arrProjects,2)
    
    Dim intPageSize
    intPageSize = 25
    
    Dim intStart
    intStart = ((intCurrentPage - 1) * intPageSize)
    
    Dim intEnd
    intEnd = intStart + intPageSize - 1
    
    Dim fldProject, fldBuyer, fldBidder, fldAverageBid
    
    Dim intCurrentRecord
    intCurrentRecord = intStart
    
    Do
    	fldProject = arrProjects(0, intCurrentRecord)
    	fldBuyer = arrProjects(1, intCurrentRecord)
    	fldBidder = arrProjects(2, intCurrentRecord)
    	fldAverageBid = arrProjects(3, intCurrentRecord)
    
    	Response.Write("<p>" & fldProject & "</p>" & vbCrLf)
    	Response.Write("<p>" & fldBuyer & "</p>" & vbCrLf)
    	Response.Write("<p>" & fldBidder & "</p>" & vbCrLf)
    	Response.Write("<p>" & fldAverageBid & "</p>" & vbCrLf)
    
    	intCurrentRecord = intCurrentRecord + 1
    	If intCurrentRecord >= intTotalRecords Then Exit Do
    Loop While intCurrentRecord < intEnd
    
    
    When using getrows, avoid misusing the two-dimensional array built by getrows. In particular, it is not uncommon to mistake a column when setting up the fields to display.

    Solution 3: Paging via a SQL Server 7 Stored Procedure

    The third and final approach involves a stored procedure. This is the most efficient approach because, unlike ADO and getrows which both return the entire set of records to the Web server, the stored procedure returns only the records that are needed for the current page.

    Once again, the paging algorithm is roughly the same as ADO, but here the current page and the page size are passed into the stored procedure as input parameters. The stored procedure then selects the set of records needed for the current page by setting up a temporary table with an identity field and using the identity field to determine which records should be returned, given the current page and page size.

    
    CREATE PROCEDURE "sprocInformationTechnologyProjects"
    
    @Page int,
    @Size int
    
    AS
    
    DECLARE @Start int, @End int
    
    BEGIN TRANSACTION GetDataSet
    
    SET @Start = (((@Page - 1) * @Size) + 1)
    IF @@ERROR <> 0
    	GOTO ErrorHandler
    
    SET @End = (@Start + @Size - 1)
    IF @@ERROR <> 0
    	GOTO ErrorHandler
    
    CREATE TABLE #TemporaryTable
    (
    	Row int IDENTITY(1,1) PRIMARY KEY,
    	Project varchar(100),
    	Buyer int,
    	Bidder int,
    	AverageBid money
    )
    IF @@ERROR <> 0
    	GOTO ErrorHandler
    
    INSERT INTO #TemporaryTable
    SELECT ...
    // Any kind of select statement is possible with however many joins
    //  as long as the data selected can fit into the temporary table.
    IF @@ERROR <> 0
    	GOTO ErrorHandler
    
    SELECT Project, Buyer, Bidder, AverageBid
    FROM #TemporaryTable
    WHERE (Row >= @Start) AND (Row <= @End)
    IF @@ERROR <> 0
    	GOTO ErrorHandler
    
    DROP TABLE #TemporaryTable
    
    COMMIT TRANSACTION GetDataSet
    RETURN 0
    
    ErrorHandler:
    ROLLBACK TRANSACTION GetDataSet
    RETURN @@ERROR
    
    
    With SQL Server 2000, the stored procedure for paging described above can be enhanced by using a local table and a "user-defined function."

    Conclusion

    In summary, programming within a stored procedure is always superior because network traffic is kept to a minimum. Even with ASP.NET's DataGrid with in-built paging around the corner, the stored procedure approach to paging is the best when dealing with large sets of data.

    About the Author

    Stephen Lian is the principal of Active Data Online Pty Ltd, http://www.activedataonline.com.au.

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Supporting Products/Tools
    XCache
    XCache combines dynamic content caching technology with content delivery network (CDN) support options, file compression and a whole lot of manageability features to help e-businesses deliver superior web site performance and reliability. You'll appreciate the administrative ease, your visitors will appreciate increased page delivery speed.
    [Top]
    XCompress
    XCompress works by compressing outgoing text between the Web server and the client. Page response times may improve by a factor of three or more while overall bandwidth use can drop by two thirds or more.

    XCompress runs on Windows 2000 and Windows NT 4.0 and is tightly integrated with Microsoft Internet Information Server (IIS) with MMC and COM interfaces.

    [Top]
    XTune
    XTune 2.0 is the most powerful tuning application for IIS 4 or IIS 5 ever conceived. Indispensable to the enterprise and straightforward, this web tuning tool allows you to configure hidden operating system, network, Active Server Pages and Internet Information Server settings for better performance, without any additional hardware or software.

    This version scans your system more deeply, offering more performance-enhancing recommendations and greater insight into your web architecture. The Performance Wizard guides and teaches you throughout the complete tuning process, so you can learn while making your box run better than ever.

    Purchase here.

    [Top]
    Other Articles
    Aug 25, 2005 - Performance Monitoring in SharePoint Portal Server 2003
    Performance monitoring helps organizations identify performance bottlenecks. The problem is that with so many performance numbers available, how do you know which ones to watch? This article helps you identify which are the critical performance counters in a SharePoint Portal Server environment and explains how to monitor them. By monitoring performance regularly, organizations can recognize performance trends as they develop and prevent problems before they get out of hand.
    [Read This Article]  [Top]
    Aug 12, 2004 - Middle-Tier Hosting: Enterprise Services, IIS, DCOM, Web Services, and Remoting
    There is broad-reaching debate about remoting, Web services, Enterprise Services, and DCOM. In short, it is a debate about the best technology to use when implementing client/server communication in .NET. Rocky Lhotka shares his thoughts on the issue while offering clear explanations of basic application architecture terminology.
    [Read This Article]  [Top]
    May 18, 2004 - ASP.NET 2.0 Caching Features
    This article examines some of the new and exciting caching features in ASP.NET 2.0 and shows how to implement them in Web applications.
    [Read This Article]  [Top]
    Feb 12, 2004 - Case Study: Match.com
    When it came time to find a technology for its massive upgrade, Match.com chose .NET. Has the online dating service's partnership with Microsoft been as successful as the relationships it has established for many of its millions of members? Read on ...
    [Read This Article]  [Top]
    Jan 15, 2004 - Database Performance Philosophy
    Longtime 15Seconds discussion list member Tore Bostrup offers valuable advice on designing databases and applications for efficient querying.
    [Read This Article]  [Top]
    Dec 29, 2003 - Caching Oracle Data for ASP.NET Applications
    Narayan Veeramani shows how ASP.NET developers can improve application performance by caching data stored in an Oracle database and keeping the cached data in sync with the data in the Oracle database.
    [Read This Article]  [Top]
    Dec 2, 2003 - Leveraging MSMQ in ASP.NET Applications
    Ever developed a Web application that requires extensive processing? Ever had long running Web pages that often time out in the browser? Greg Huber reveals a simple technique that uses Microsoft Message Queuing (MSMQ) and the System.Messaging framework to handle long running Web processes.
    [Read This Article]  [Top]
    Mar 14, 2002 - Web Site Compression
    As IT professionals try to reduce the cost of operating their Web sites, they should consider reducing the amount of bandwidth usage. Learn how to successfully compress your HTML output and save money on your monthly bandwidth.
    [Read This Article]  [Top]
    Feb 6, 2002 - The Just Two Theory on Web Servers
    Maintaining a large Web farm is both costly and unnecessary. Learn how to reduce your Web farm to just two servers in this controversial article by Wayne Berry.
    [Read This Article]  [Top]
    Aug 14, 2001 - NT Authentication's Impact on Connection Pooling
    Steve Witkop examines OLE DB and ODBC connection pooling when used with Microsoft NT LAN Manager Web server authentication.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers