Search Tools Links Login

Database paging in ASP


I wanted to add paging code in my project.I've seen all other codes on the site but they are not at all worth for me. Now I've written a code which is very easy to understand and can be used by any student or professional in their projects.

Original Author: Vivek Tanaji Kulthe

Assumptions

This program assumes that you have dsn = 'myDSN' or replace 'myDSN' with your existing DSN.

Returns

This program displays 5 records at a time from any database. You can change no. of records per page by changing the value of iPageSize variable

Code

<%
Const iPageSize=5 'How many records to show
Dim CPage 'Current Page No.
Dim Cn 'Connection Object
Dim Rs 'Recordset Object
Dim TotPage 'Total No. of pages if iPageSize records are displayed per page.
Dim i 'Counter
CPage=Cint(Request.Form("CurrentPage")) 'get CPage value from form's CurrentPage field
Select Case Request.Form("Submit")
Case "Previous" 'if prev button pressed
CPage = Cint(CPage) - 1 'decrease current page
Case "Next" 'if next button pressed
CPage = Cint(CPage) + 1 'increase page count
End Select
Set Cn=Server.CreateObject("ADODB.Connection") 'create connection
Cn.CursorLocation = 3
Cn.Open "myDSN"
Set Rs=Server.CreateObject("ADODB.Recordset") 'create recordset
Rs.Open "Select * from studentmaster",Cn,2,2
Rs.PageSize=iPageSize
If CPage=0 then CPage=1 'initially make current page = first page
If Not(Rs.EOF) Then Rs.AbsolutePage=CPage 'specifies that current record resides in CPage
TotPage=Rs.PageCount 'stores total no. of pages
%>


Database paging example


by Vivek Kulthe (vivekkulthe@yahoo.com)



<%
Response.Write("") 'display title for table
%>
<%
For i=1 to Rs.PageSize
Response.Write ("") 'display table records upto PageSize
Rs.MoveNext
If Rs.EOF Then Exit For
Next
'close all connections and recordsets
Rs.Close
Cn.Close
Set Rs = Nothing
Set Cn = Nothing
%>
" & Rs.Fields(1).Name & "" & Rs.Fields(2).Name & "
" & Rs(1) & "" & Rs(2) & "



Page <%=CPage %> of <%=TotPage %>




" Method=POST>


<% If CPage > 1 Then %>

<% End IF%>
<% If CPage <> TotPage Then %>

<% End If %>


About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 65 times

Categories

ASP/ HTML

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.