classic asp recordset

Posted on 2011-09-03
Medium Priority
Last Modified: 2012-05-12
Attached code is working fine. but I would like to improve a little on paging
How if I have 1000 record, and have pagesize=5
It will show number from 1 to 200
What I really want to do is like

<< 1 ,2,3,4,5 >>
If  ">>" Is clicked, it will should 6,7,8,9,10...and etc.

Does it make sense and possible to make it happen?
The codes is long but you just have to focus last part dealing with paging.

<!--#include virtual="/RideFinder/Adovbs.asp"--> 

' Our SQL code - overriding values we just set
' Comment out to use Access
CONN_USER = "sa"
CONN_PASS = "ss"

' Declare our vars
Dim iPageSize       'How big our pages are
Dim iPageCount      'The number of pages we get back
Dim iPageCurrent    'The page we want to show
Dim strOrderBy      'A fake parameter used to illustrate passing them
Dim strSQL          'SQL command to execute
Dim objPagingConn   'The ADODB connection object
Dim objPagingRS     'The ADODB recordset object
Dim iRecordsShown   'Loop controller for displaying just iPageSize records
Dim I               'Standard looping var

' Get parameters
iPageSize = 5 ' You could easily allow users to change this

' Retrieve page to show or default to 1
If Request.QueryString("page") = "" Then
	iPageCurrent = 1
	iPageCurrent = CInt(Request.QueryString("page"))
End If

' Make sure the input is one of our fields.
strOrderBy = LCase(Request.QueryString("order"))
Select Case strOrderBy
	Case "last_name", "first_name", "sales"
		' A little pointless, but...
		strOrderBy = strOrderBy
	Case Else
		strOrderBy = "id"
End Select

' Build our SQL String using the parameters we just got.
'strSQL = "SELECT * FROM sample ORDER BY " & strOrderBy & ";"
SQL = SQL & " f.AddressID, f.Address AS frAddress, f.City AS frCity, f.Zip AS frZip, "
SQL = SQL & " d.Address AS toAddress, d.City AS toCity, d.Zip AS toZip, "
SQL = SQL & " g.VanpoolID, g.MilesToWork, g.PickupAddressID, "
SQL = SQL & " p.EmployerName, p.VanpoolID AS Expr1, "
SQL = SQL & " v.VanNumber "
SQL = SQL & " FROM Vanpool AS p INNER JOIN "
SQL = SQL & " Passenger AS g ON p.VanpoolID = g.VanpoolID INNER JOIN "
SQL = SQL & " Van AS v ON p.VanID = v.VanID INNER JOIN "
SQL = SQL & " Address AS d ON p.DestinationID = d.AddressID INNER JOIN "
SQL = SQL & " Address AS f ON g.PickupAddressID = f.AddressID " 
SQL = SQL & " WHERE (d.City ='" & Destination & "') "
SQL = SQL & " ORDER BY f.AddressID, v.VanNumber " 
' Now we finally get to the DB work...
' Create and open our connection
Set objPagingConn = Server.CreateObject("ADODB.Connection")

' Create recordset and set the page size
Set objPagingRS = Server.CreateObject("ADODB.Recordset")
objPagingRS.PageSize = iPageSize

' You can change other settings as with any RS
'objPagingRS.CursorLocation = adUseClient
objPagingRS.CacheSize = iPageSize

' Open RS
objPagingRS.Open strSQL, objPagingConn, adOpenStatic, adLockReadOnly, adCmdText

' Get the count of the pages using the given page size
iPageCount = objPagingRS.PageCount

' If the request page falls outside the acceptable range,
' give them the closest match (1 or max)
If iPageCurrent > iPageCount Then iPageCurrent = iPageCount
If iPageCurrent < 1 Then iPageCurrent = 1

' Check page count to prevent bombing when zero results are returned!
If iPageCount = 0 Then
	Response.Write "No records found!"
	' Move to the selected page
	objPagingRS.AbsolutePage = iPageCurrent

	' Start output with a page x of n line
	<font size="+1">Page <strong><%= iPageCurrent %></strong>
	of <strong><%= iPageCount %></strong></font>
	' Spacing
	Response.Write vbCrLf

	' Continue with a title row in our table
	Response.Write "<table border=""1"">" & vbCrLf

	' Show field names in the top row
	Response.Write vbTab & "<tr>" & vbCrLf
	For I = 0 To objPagingRS.Fields.Count - 1
	    Response.Write vbTab & vbTab & "<th>"
	    Response.Write objPagingRS.Fields(I).Name
	    Response.Write "</th>" & vbCrLf
	Next 'I
	Response.Write vbTab & "</tr>" & vbCrLf

	' Loop through our records and ouput 1 row per record
	iRecordsShown = 0
	Do While iRecordsShown < iPageSize And Not objPagingRS.EOF
		Response.Write vbTab & "<tr>" & vbCrLf
		For I = 0 To objPagingRS.Fields.Count - 1
		    Response.Write vbTab & vbTab & "<td>"
		    Response.Write objPagingRS.Fields(I)
		    Response.Write "</td>" & vbCrLf
		Next 'I
		Response.Write vbTab & "</tr>" & vbCrLf

		' Increment the number of records we've shown
		iRecordsShown = iRecordsShown + 1
		' Can't forget to move to the next record!

	' All done - close table
	Response.Write "</table>" & vbCrLf
End If

' Close DB objects and free variables
Set objPagingRS = Nothing
Set objPagingConn = Nothing

' Show "previous" and "next" page links which pass the page to view
' and any parameters needed to rebuild the query.  You could just as
' easily use a form but you'll need to change the lines that read
' the info back in at the top of the script.
If iPageCurrent > 1 Then
	<a href="Testpaging2.asp?page=<%= iPageCurrent - 1 %>&order=<%= Server.URLEncode(strOrderBy) %>">[&lt;&lt; Prev]</a>
End If

' You can also show page numbers:
For I = 1 To iPageCount
	If I = iPageCurrent Then
		<%= I %>
        <a href="Testpaging2.asp?page=<%= I %>&order=<%= Server.URLEncode(strOrderBy) %>"><%= I %></a>
	End If
Next 'I

If iPageCurrent < iPageCount Then
	<a href="Testpaging2.asp?page=<%= iPageCurrent + 1 %>&order=<%= Server.URLEncode(strOrderBy) %>">[Next &gt;&gt;]</a>
End If
response.write "<br>"
response.write "Total Page: " & iPageCount & "<Br>" & cint(iPageCount / 5)


Open in new window

Question by:Webboy2008
LVL 61

Accepted Solution

Kevin Cross earned 2000 total points
ID: 36479572

Looks like your issue may be in the loop displaying the results. Check section three of this article on CodeProject: http://www.codeproject.com/KB/database/rspaging.aspx

If you have difficulty, please advise; however, I find the CodeProject page easy to follow. Hoepfully you do too. 4GuysFromRolla.com has good examples. They are doing it the way you are with a counter, so might be a good double check for what is wrong. As I said, I believe it is in the loop, i.e., they use objPagingRS.PageSize whereas you are using iPageSize.

LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 36482295

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question