When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















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

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Saturday, December 12, 1998

Paging through Database Results N Records at a Time
By Scott Mitchell


A More Up-To-Date Paging Solution
For a more up-to-date look at efficiently paging through large resultsets, see Greg Hamilton's article A More Efficient Method for Paging Through Large Result Sets.

One of ASP's great assests is that it can connect to a database on the server and report the results of a query to a user. If the queries are small, the page loads quickly, and there isn't too much data to wade through. However, if there are hundreds of thousdands of results being returned by the database, attempting to display all that data on one page would not only be difficult for the reader to utilize, but would also be very slow to execute!

There is a solution, however. Using ADO you can page through data N records at a time. The records are retrieved N at a time, so the query time will be shortened considerably, plus the data will be easier to examine and use. We will be using ClientSide cursors (meaning that the recordset cursor will be stored on the client's side, as opposed on the server (which is the default), the CacheSize property (to specify how many records to cache on the cursor), and the paging properties: AbsolutePage, PageSize, and PageCount.

Let's start by looking at some code. Remember to include ADOVBS.inc. (If you don't have it, it can be downloaded here. This program assumes that ADOVBS is located in your web application's root directory and that the file is named "thisfile.asp".)

<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<!--#include virtual="/ADOVBS.inc"-->
<%
    'Set how many records per page we want
    Const NumPerPage = 10

    'Retrieve what page we're currently on
    Dim CurPage
    If Request.QueryString("CurPage") = "" then
        CurPage = 1 'We're on the first page
    Else
        CurPage = Request.QueryString("CurPage")
    End If

    Dim conn
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.Open "DSN=MyDB"

    'Explicitly Create a recordset object
    Dim rs
    Set rs = Server.CreateObject("ADODB.Recordset")

    'Set the cursor location property
    rs.CursorLocation = adUseClient

    'Set the cache size = to the # of records/page
    rs.CacheSize = NumPerPage

    'Open our recordset
    Dim strSQL
    strSQL = "SELECT Name,Salary FROM Employee ORDER BY Name"
    rs.Open strSQL, Conn

    rs.MoveFirst
    rs.PageSize = NumPerPage

    'Get the max number of pages
    Dim TotalPages
    TotalPages = rs.PageCount

    'Set the absolute page
    rs.AbsolutePage = CurPage

    'Counting variable for our recordset
    Dim count
%>

<HTML>
<BODY>
<B>Name - Salary</B><BR>
<%
    'Set Count equal to zero
    Count = 0
    Do While Not rs.EOF And Count < rs.PageSize
        Response.Write(rs("Name") & " - " & rs("Salary") & "<BR>")
        Count = Count + 1
        rs.MoveNext
    Loop

    'Print out the current page # / total pages
    Response.Write("Page " & CurPage & " of " & TotalPages & "<P>")

    'Display Next / Prev buttons
    if CurPage > 1 then
        'We are not at the beginning, show the prev button
        Response.Write("<INPUT TYPE=BUTTON VALUE=PREV ONCLICK=""document.location.href='thisfile.asp?curpage=" & curpage - 1 & "';"">")
    End If

if CInt(CurPage) <> CInt(TotalPages) then
        'We are not at the end, show a next button
        Response.Write("<INPUT TYPE=BUTTON VALUE=NEXT ONCLICK=""document.location.href='thisfile.asp?curpage=" & curpage + 1 & "';"">")
    End If

%>

</BODY>
</HTML>

Let's quickly go over some of those ADO properties we used. Setting PageSize to an integer value tells ADO how many records per page you are wanting. PageCount, then, tells you how many pages total there are. Obviously you need to set PageSize before PageCount will yield a sensible result. (This is not strictly true, since PageSize defaults to 10, but always be sure to set PageSize explicitly if you plan on using paging!) AbsolutePage is a value you can set to tell ADO to get a certain page. So, if you have pages x through y with N records per page, you could say, AbsolutePage = M, and the recordset cursor will start at the M*Nth record (assuming M < y).

The CacheSize tells ADO to cache N number of rows on the cursor. Since we chose to have 10 records per page, I set the cache size to 10. ADO will grab the first 10 records, and cache those. It won't hit the database until you request any past the cached limit. Since we are only displaying 10 records per page, we are only having one quick database hit per page. The caching property is vital to the performance of the script above.

Hopefully you now understand how to page through data using ADO / ASP. Be forewarned, you can only page through certain cursor types. For example, you cannot page through a forward-only cursor (which is the default for server side cursors). Paging allows a quick and visually appealing way for you to display results from a query to your users!

Happy Programming!

  • By Scott Mitchell


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • 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

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Windows Server 2008
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES