[pic]

[ Home ] [ Discuss ] [ Documentation ]
Search:

 

 
ASP Source Code:
<%@ Language = VBScript %>
<%option explicit%>
<html>
<head>
<title>SQL Example</title>
<link rel="stylesheet" href="/core.css">
</head>

<body>
<!--#include virtual="header.html"--> 

<center><h1>Paging through records</h1></center>

<p>
One of issues of interest to application developers is how to page through a 
large result set. The following example demonstrates this concept. Click on
the next button to page through the records of the EMPLOYEE_MASTER table.
The <a href="source.asp?file=<%= right(Request.ServerVariables("PATH_INFO"), len(Request.ServerVariables("PATH_INFO"))-instrrev(Request.ServerVariables("PATH_INFO"), "/")) %>">source</a> to this sample page is also available.
</p>

<p>
<%
dim offset, oConn, oRs, SQL, i, url, tmp, orig_offset,qs

orig_offset = Request.querystring("offset")

if (orig_offset = "") then
  offset = 0
else
  ' Causes an error 800a000d if there is more than one offset
  offset = Int(orig_offset)
  if offset < 0 then
    offset=0
  end if
end if

SET oConn = Server.CreateObject("ADODB.Connection")
if oConn = null then
  abort()
end if

oConn.Open "DSN=qcd;UID=qsql;Password=pa$$w0rd;"
if oConn = null then
  abort()
end if

SQL="select * from EMPLOYEE_MASTER"
set oRs = oConn.Execute(SQL)

' skip to the current page
oRs.Move offset 

response.write "records " & offset+1 & " to " & offset+11 & "</br><br/></p>"

' display windowed result set
WHILE NOT oRs.EOF and i<10
  response.write oRs.Fields("FIRST_NAME").Value & " "  & _
    oRs.Fields("LAST_NAME").Value & "<br/>" & vbCrLf
  oRs.MoveNext
  i = i + 1
WEND

response.write "</p>"

qs = replace(url, "&offset=" & orig_offset, "") 'in case its in the middle 
qs = replace(url, "offset=" & orig_offset, "")  'in case its at the beginning
url = right(Request.ServerVariables("PATH_INFO"), len(Request.ServerVariables("PATH_INFO"))-instrrev(Request.ServerVariables("PATH_INFO"), "/")) & qs

' take the guess work out of whether offset is the first in the querystring
' also deal with poorly fashioned urls
if len(qs) = 0 then
  url = url & "?offset="
else
  url = url & "&offset="
end if


if offset > 0 then
  response.write "<a HREF=" & url & (offset-10) & "><img border='0' src='arrow-left.gif'></a>"
end if
response.write " Previous 10"

response.write "&nbsp;|&nbsp;"

response.write "Next 10 "
if (oRs.EOF = false) then
  response.write "<a HREF=" & url & (offset+10) & "><img border='0' src='arrow-right.gif'></a>"
end if

oConn.Close
set oConn = Nothing
%> 
</p>

<!--#include virtual="footer.html"-->
</body>
</html>
© 1999-2019, Qantel Technologies, Inc.