Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
'**************************************
' Name: Database paging in ASP
' Description:I wanted to add paging cod
' e in my project.I've seen all other code
' s on the site but they are not at all wo
' rth for me. Now I've written a code whic
' h is very easy to understand and can be
' used by any student or professional in t
' heir projects.
' By: Vivek Tanaji Kulthe
'
' Returns:This program displays 5 record
' s at a time from any database. You can c
' hange no. of records per page by changin
' g the value of iPageSize variable
'
' Assumes:This program assumes that you
' have dsn = 'myDSN' or replace 'myDSN' wi
' th your existing DSN.
'
'This code is copyrighted and has
' limited warranties.Please see [URL unfurl="true"]http://w[/URL]
' ww.Planet-Source-Code.com/xq/ASP/txtCode
' Id.8122/lngWId.4/qx/vb/scripts/ShowCode.
' htm
'for details.
'**************************************
<%
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
%>
<HTML>
<BODY>
<H2>Database paging example</H2>
by Vivek Kulthe (<A href = "mailto:vivekkulthe@yahoo.com">vivekkulthe@yahoo.com</A>)<P>
<TABLE BORDER = 1>
<%
Response.Write("<TR><TD><B>" & Rs.Fields(1).Name & "</TD><TD><B>" & Rs.Fields(2).Name & "</TD></TR>") 'display title For table
%>
<%
For i=1 To Rs.PageSize
Response.Write ("<TR><TD>" & Rs(1) & "</TD><TD>" & Rs(2) & "</TD><TR>") '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
%>
</TABLE>
<BR>
Page <%=CPage %> of <%=TotPage %><P>
<!--'store current page value In hidden Type and display next-prev buttons-->
<FORM Action="<%=Request.ServerVariables("SCRIPT_NAME") %>" Method=POST>
<INPUT Type=Hidden name="CurrentPage" Value="<%=CPage%>" >
<% if CPage > 1 Then %>
<INPUT type=Submit Name="Submit" Value="Previous">
<% End if%>
<% if CPage <> TotPage Then %>
<INPUT type=Submit Name="Submit" Value="Next">
<% End if %>
</FORM>
</BODY>
</HTML>