Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Article Categories
.NET  .NET
  ASP (15)
  ASP.NET (26)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Latest Forum Activity
what is the right code to link the asp page t..
by amylisa on 22 Jul 2008 Go To Post

Can Loader.asp Get Form Elements
by azziham on 14 Jul 2008 Go To Post

Good asp resource sites
by codemylife on 3 Jul 2008 Go To Post

Re: Unable to insert data in an Access databa..
by asia on 3 Jul 2008 Go To Post

Re: problem with do while loop
by idsanjeev on 30 Jun 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ADO : Using ASP pages to page through Recordsets
 

Using ASP pages to page through Recordsets
by Faisal Khan.

Introduction
In this article we will learn how to use ASP pages to page through recordsets.

What is paging ?
Paging is a very useful way of presenting data. It allows you to show 5, 10 or whatever number of records you want per page and then show typical 'first page', 'last page', 'previous page', 'next page' etc buttons to navigate between pages.

How do I do it ?
This is what we will learn in subsequent pages.

What do I need to know ?
I only expect that you know at least the basics of accessing the database and presenting it's data on the ASP page. If you are not comfortable with it then kindly read Accessing the Database via ASP tutorial first.

Building paging.asp page
Open text pad or whatever program you use to build ASP pages and create a new page. Copy and paste the following code into it and then save it as 'paging.asp' :

<% Option Explicit

' ADO constants used in this page
Const adOpenForwardOnly = 0
Const adLockReadOnly = 1
Const adCmdTableDirect = &H0200
Const adUseClient = 3
%>
<html>
<head>
 <style>
  body { font-family : Verdana; font-size : 8pt; }
  a { font-family : Verdana; font-size : 8pt;
   text-decoration : none; }
 </style>
</head>

<body>
<%
 Dim connStr
  connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
   Server.MapPath("paging.mdb")

 Dim rs
  Set rs = Server.CreateObject("ADODB.Recordset")

  rs.PageSize = 5
  rs.CacheSize = 5
  rs.CursorLocation = adUseClient

 rs.Open "Names", connStr, adOpenForwardOnly, adLockReadOnly, _
  adCmdTableDirect
 
 If Len(Request("pagenum")) = 0  Then
  rs.AbsolutePage = 1
 Else
  If CInt(Request("pagenum")) <= rs.PageCount Then
   rs.AbsolutePage = Request("pagenum")
  Else
   rs.AbsolutePage = 1
  End If
 End If

 Dim abspage, pagecnt
  abspage = rs.AbsolutePage
  pagecnt = rs.PageCount

 If Not rs.EOF Then
  Response.Write "PageCount : " & rs.PageCount & "<br>" & vbcrlf
  Response.Write "Absolute Page : " & rs.AbsolutePage & "<br>" & vbcrlf

  Response.Write "Total number of records : " & rs.RecordCount
  Response.Write "<br><br>" & vbcrlf

  Dim fldF, intRec

  Response.Write "<table border=1 align=center cellpadding=3 "
  Response.Write "cellspacing=0><thead><tr>"
  For Each fldF in rs.Fields
   Response.Write "<td>" & fldF.Name & "</td>"
  Next
  Response.Write "</tr></thead><tbody>"

  For intRec=1 To rs.PageSize
   If Not rs.EOF Then
    Response.Write "<tr>"
    For Each fldF in rs.Fields
     Response.Write "<td>" & fldF.Value & "</td>"
    Next
    Response.Write "<tr>"
    rs.MoveNext
   End If
  Next
  Response.Write "</tbody></table><p>"

Note
Note that this is not complete ASP page so don't start running it. On the next page we will add some more code to it and then you can run it. Also remember that you can download the complete working page and the database at the end of this article.

Explanation
Most of the code given above will already be familiar to you. I will only explain the parts of it which involve paging through recordsets.

	rs.PageSize = 5

Ok first let me summarize to what we have done till getting to the above code. We have defined the connection string we will use to connect to the database. Then we have created the Recordset object but haven't yet opened the database. Now the above code, before opening the database we define certain properties of the Recordset object which will enable us to page through recordset later. It is important that you understand these properties now so that later when you create paging in your own pages you don't forget what you have learned here.

PageSize should be set to the number of records you want to show per page. If you want to show 5 records then set it to 5 and if want to show 10 records then set it to 10 and so on.

	rs.CacheSize = 5

CacheSize is totally optional as far as paging of recordset is concerned. It should be set to the number of records to be cached in the memory.

	rs.CursorLocation = adUseClient

Now this is a must. CursorLocation property should be set to 'adUseClient' or paging won't work.

	If Len(Request("pagenum")) = 0  Then
		rs.AbsolutePage = 1
	Else
		If CInt(Request("pagenum")) <= rs.PageCount Then
			rs.AbsolutePage = Request("pagenum")
		Else
			rs.AbsolutePage = 1
		End If
	End If

PageCount is a read only property which tells the total number of pages to be shown containing the records. For example if you have a database with 20 records and you have set PageSize to 5 then value of PageCount will be 4.

Ok now have set the PageSize to 5 and our sample database in this article contains 17 records so value of PageCount will be 4 with last ( 4th ) page showing only 2 records. Meaning there by that we have 4 pages of recordsets with us. But how to show the page we want ? This is where we need AbsolutePage property. It allows you to select the recordset page you want to show to the user. So if you want to show the first recordset page then set AbsolutePage to 1. You can set it to any page value between 1 and total number of recordset pages which you can get by PageCount property.

This is exactly what we have done in the above code. We are asking for the query string 'pagenum'. If it is there, that is we have given it then set AbsolutePage to it otherwise set it to 1.

	Dim abspage, pagecnt
		abspage = rs.AbsolutePage
		pagecnt = rs.PageCount

	If Not rs.EOF Then
		Response.Write "PageCount : " & rs.PageCount & _
			"<br>" & vbcrlf
		Response.Write "Absolute Page : " & rs.AbsolutePage & _
			"<br>" & vbcrlf

		Response.Write "Total number of records : " & rs.RecordCount
		Response.Write "<br><br>" & vbcrlf

Here we output the PageCount, AbsolutePage and RecordCount properties of Recordset object to show you how they work. It is not important as far as paging is concerned. You can use them if you want in your paging pages or simply ignore them.

	For intRec=1 To rs.PageSize
		If Not rs.EOF Then
			Response.Write "<tr>"
			For Each fldF in rs.Fields
				Response.Write "<td>" & _
					fldF.Value & "</td>"
			Next
			Response.Write "<tr>"
			rs.MoveNext
		End If
	Next
	Response.Write "</tbody></table><p>"

Now this is where we show the records according to the AbsolutePage property. We use two loops to iterate through the records. One loop loops through the number of records to show and other one shows all the fields in the record.

Completing paging.asp page
Copy the following code and paste it into the paging.asp page below the code you pasted in the last page :


 ( 1 Remaining ) Next

Comments/Questions ( Threads: 21, Comments: 23 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. Paging through recordsets
  2. Showing only the first record
  3. Help pls....
  4. Using ASP pages to page through Recordsets
  5. Great Article
  6. HELP! Using the paging ASP code for inter join record sets?
  7. Error ADODB.Recordset error '800a0bcd'
  8. asp
  9. Having problem when inserting a sql statement
  10. Using SELECT in your example
  11. urgent please
  12. Just Wondering
  13. Watch the provider
  14. How to show table records, based on another query results? ( 1 Reply )
  15. what a guy hay
  16. Combining 2 of your ASP codes
  17. using a query
  18. Help please paging
  19. Drill Down
  20. Displaying records in a table format
  21. Shorting? ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2008 Stardeveloper.com, All Rights Reserverd.