VB icon

Database paging in ASP

Email
Submitted on: 1/6/2015 3:22:00 PM
By: Vivek Tanaji Kulthe (from psc cd)  
Level: Intermediate
User Rating: By 8 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 635
 
     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.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Database paging in ASP
' Description: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.
' By: Vivek Tanaji Kulthe (from psc cd)
'
' 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
'
' Assumes:This program assumes that you have dsn = 'myDSN' or replace 'myDSN' with your existing DSN.
'**************************************

<% 
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>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.