Library code snippets

Paginate a recordset

Although SQL Server an Access support the TOP keyword to limit the amount of records returned, you cannot specify a starting value; making it unsuitable for use when paginating a recordset (ie displaying records over multiple pages, as in a web directory). Fortunately, ADO provides an alternative:

'create a recordset object
Set rItems = Server.CreateObject("ADODB.Recordset")

'set the cursor location and type
rItems.CursorLocation = 3' adUseClient
rItems.CursorType = 3 'adOpenStatic

'number of rows to cache at a time. Should be set to the same as PageSize
rItems.CacheSize = 10

'number of items to display per 'page'
rItems.PageSize = 10

'execute the SQL query, and keep the recordset open
'you cannot use the Execute statement for this.

rItems.Open sSQL, cConn

'check if empty
If rItems.EOF Then
    'no rows
Else

    'set current page
    rItems.AbsolutePage = Request.QueryString("page")

    'get the total number of records
    nItemCount = rItems.RecordCount

    'get the number of pages
    nPageCount = rItems.PageCount

    'loop through an display the items in the recordset
    Do While Not rItems.EOF and nItem < rItems.PageSize

        'do something
        rItems.MoveNext 'move to the next record
        nItem=nItem+1 'increment count

    Loop
End If

'close the recordset
rItems.Close

'and destroy the object...
Set rItems = Nothing

Comments

  1. 03 Nov 2004 at 10:16

    Dear James


    Kindly give the example

  2. 03 Nov 2004 at 10:14

    Error Type:
    ADODB.Recordset (0x800A0BB9)
    Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
    /country/result.asp, line 80

  3. 07 Oct 2003 at 09:01

    Hi James Crowley


    Thanks for your sample code, but if you provide with sample ASP page that will call this sample code. May be you can provide asp page that will display navigation buttons like first page, next page, previous page etc., and explain the code, how this will function.


    Expecting reply.


    Thanks in advance..


    Regards,
    Pargunan P

  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Paginate a recordset.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • ASP.NET Caching and Performance

    Steve Smith, owner of ASP Alliance and Lake Quincy Media joins us today to teach us about some hidden gems in ASP.NET caching and performance. Steve’s expertise in this area comes from first-hand experience as Lake Quincy’s ad system serves over 60 requests per second and handles over 150 million...

Related jobs