Many times after executing a query we get large number of records back and need to display certain number of records per page. Typically this occurs while providing search or find functionality. Above task can be accomplished by using ADO recordset's paging properties. Following properties are of our interest :- PageSize which sets the number of records per ADO recordset page
- PageCount which gives total number of pages
- AbsolutePage which gets/sets the current page
Now,we will develop a sample script to demonstrate the usage. For the purpose of this example you should have biblio.mdb that comes with VB. For the sake of simplicity error handling and fine tuning is omitted. Following steps are involved in the process : - Declare ADO recordset variable
- Set cursor type, location and pagesize properties
- Open recordset
- Display a list of page nummbers as hyperlinks so that when user clicks any link he will be presented with the records from that page
- Set current page using AbsolutePage property
- Display record contents from current page
Example Code :
<%@ Language=VBScript %> <% Response.Buffer=true %> <HTML> <HEAD> <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> </HEAD> <BODY> <% dim rs,i,strConn,strTemp set rs=server.CreateObject("adodb.recordset") rs.CursorLocation=3 'clientside rs.CursorType=3 'staticrecordset rs.PageSize=100 strConn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=D:\Microsoft Visual Studio\VB98\Biblio.mdb" rs.Open "select * from authors",strConn Response.Write "<table><tr>" for i=1 to rs.PageCount Response.Write "<td><ahref=paging.asp?pg=" & i & ">" & i & "</a></td>" next Response.Write "</tr></table><br>" if Request.QueryString("pg")="" then rs.AbsolutePage=1 For i=1 to 100 Response.Write rs.Fields("author") & "<br>" rs.MoveNext Next else rs.AbsolutePage=cint(Request.QueryString("pg")) For i=1 to 100 Response.Write rs.Fields("author") & "<br>" rs.MoveNext Next end if %> </BODY> </HTML> Let's examine the code - dim rs,i,strConn,strTemp
set rs=server.CreateObject("adodb.recordset")
First we declared variables for recordset,connection string and counter Next we reated recordset object using CreateObject method
- rs.CursorLocation=3
rs.CursorType=3 rs.PageSize=100
The cursorlocation is set to client-side and cursor type is set to static cursor. Then we set the PageSize property to 100 which sets records per page to 100.
- rs.Open "select * from authors",strConn
The recordset is then opened.
- Response.Write "<table><tr>"
Response.Write rs.PageCount for i=1 to rs.PageCount Response.Write "<td><ahref=paging.asp?pg=" & i & ">" & i & "</a></td>" next Response.Write "</tr></table>"
This code simply puts the links for available pages in a table. The total pages can be found out by PageCount property
- if Request.QueryString("pg")="" then
rs.AbsolutePage=1 For i=1 to 100 Response.Write rs.Fields("author") & "<br>" rs.MoveNext Next else rs.AbsolutePage=cint(Request.QueryString("pg")) For i=1 to 100 Response.Write rs.Fields("author") & "<br>" rs.MoveNext Next end if
This code checks for page number selected by the user.First time when we visit that page it will be blank and hence we set the AbsolutePage property to 1. If user selects any page that page number will be assigned to AbsolutePage property. This causes the record pointer to move to the start of the page. We then display author names by scrolling through the recordset.
In live usage, code for checking ADO errors and checking recordset EOF can be added easily. |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Database Code Articles More By Bipin Joshi developerWorks - FREE Tools! | David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
| | | | Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
| | | | Download the Rational Application Developer (RAD) v7.5 open beta code and start developing applications for the JEE5 standard which features EJB3.0, JPA, JSF 1.2, JSP 2.1 and Servlet 2.5 standards. When you use this beta you will see how you can increase developer productivity for already existing applications with improved support for refactoring, as well as adding new features to existing applications. In addition, the beta provides tooling for JD Edwards, Oracle, SAP, Siebel and PeopleSoft to improve the developer productivity with these enterprise systems. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
| | | | Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code. FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
| | | | It's a good time to be a Web developer. You've never had more choices in terms of technologies. There are so many great open source Web servers, databases, programming languages, and development frameworks. No matter what combination of technologies you prefer to work with, there is a single integrated development environment (IDE) that can increase your productivity: Eclipse. Here in Part 3, we introduce the RDT and RadRails Eclipse plug-ins and show you how to get these plug-ins and start using them. You will learn how to use RadRails to do many common Ruby on Rails development tasks. FREE! Go There Now!
| | | | The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now. FREE! Go There Now!
| | | | In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |