Solved

RECORDSET PAGING

Posted on 2006-06-26
4
1,386 Views
Last Modified: 2013-12-09
I am using FIRST and SKIP (Firebird SQL) for recordset paging to reduce the time it takes for my records to travel from the server to the client.

This works great because instead of sending thousands of records across the wire to be paged by my ASP code, only the 10, 20, 50 etc. records that are selected will be sent.

In order to calculate how many records to skip, I need to get a count of how many records are in the recordset. That means I am running a SQL statement to get the recordcount and then another SQL statement to actually get the records. There must be a way to combine these statements? Currently, I am only getting the recordcount on the first run but this won't account for newly updated transactions.

<%
'Records per page
      recordsPerPage = Request("recordsPerPage")
      If recordsPerPage = "" Then recordsPerPage = 10

'Current page
      currentPage = Request("currentPage")
      If currentPage = "" Then currentPage = 1

' Create and Open Database/Recordset Object
      Call dbConn(objConn)
      Call openRS(objRS)
      objRS.CursorLocation = 3
      objRS.CacheSize = recordsPerPage
      
' How many records in recordset? This is what I would like to replace/combine with following SQL statement
' Only runs on the first time through but doesn't account for new transactions
      If Request("howManyRecords") = "" Then
            objRS.Open "Select COUNT(AGPDR) AS howManyRecords From TABLEA", objConn
            howManyRecords = objRS("howManyRecords")
            objRS.Close      
      Else
            howManyRecords = Request("howManyRecords")
      End If
      
'Calculate records to skip
      totalPages = (howManyRecords/recordsPerPage)
      temp = fix(totalPages)
      if totalPages-temp <> 0 then totalPages = temp+1 else totalPages=temp
      numSkip =  (currentPage * recordsPerPage) - recordsPerPage
      If CINT(numSkip) > CINT(howManyRecords) then numSkip = howManyRecords - recordsPerPage      

' Open database and retrieve records
      strSQL = "Select FIRST " &CINT(recordsPerPage)& " SKIP " &CINT(numSkip)& " FIELD1, FIELD2, FIELD3 "
      strSQL = strSQL & "FROM TABLEA"
      objRS.Open strSQL, objConn


response.write "Number of records: " &howManyRecords& "<br>"
response.write "Total Pages: " &totalPages& "<br>"
response.write "Current Page: " &currentPage& "<br>"
response.write "Skip: " &numSkip& "<br><br>"

%>

<table border="0" cellspacing="0" cellpadding="2">
 <tr><td colspan="3"></td></tr>
 <tr>
  <td><b>COLUMN1</b></td>
  <td><b>COLUMN2</b></td>
  <td><b>COLUMN3</b></td>
 </tr>

<%

Do While Not objRS.EOF
%>    
 <tr><td colspan="3"></td></tr>
 <tr>
  <td><b><%=FIELD1%></a></b></td>
  <td><b><%=FIELD2%></a></b></td>
  <td><b><%=FIELD3%></a></b></td>
 </tr>

<%
objRS.MoveNext
Loop

Call closeRS(objRS)
Call closeDB(objConn)  

%>

</table>

<!-- Begin Page Navigation -->
<form name="MovePage" method="POST" action="thisPage.asp">
      <input type="hidden" name="howManyRecords" value="<%=howManyRecords%>">
      <input type="hidden" name="totalPages" value="<%=totalPages%>">

      <!-- The dropdown box for selecting the number of records to display -->
      <select name="recordsPerPage" onchange="MovePage.submit()">
            <option value="10">10</option>
              <option value="20">20</option>
                   <option value="50">50</option>
            </select>
<%    
'Display Next / Prev buttons
If currentPage > 1 then
      'Show previous buttons
      Response.Write("<INPUT TYPE=BUTTON VALUE=&lt;&lt; ONCLICK=""document.location.href='thisPage.asp?currentPage=1&recordsPerPage="&recordsPerPage&"&howManyRecords="&howManyRecords&"';""> ")
      Response.Write("<INPUT TYPE=BUTTON VALUE=&lt; ONCLICK=""document.location.href='thisPage.asp?currentPage="&currentPage-1&"&recordsPerPage="&recordsPerPage&"&howManyRecords="&howManyRecords&"';""> ")
End If

If CInt(currentPage) <> CInt(totalPages) then
      'Show next buttons
      Response.Write("<INPUT TYPE=BUTTON VALUE=&gt; ONCLICK=""document.location.href='thisPage.asp?currentPage="&currentPage+1&"&recordsPerPage="&recordsPerPage&"&howManyRecords="&howManyRecords&"';""> ")
      Response.Write("<INPUT TYPE=BUTTON VALUE=&gt;&gt; ONCLICK=""document.location.href='thisPage.asp?currentPage="&totalPages&"&recordsPerPage="&recordsPerPage&"&howManyRecords="&howManyRecords&"';""> ")
End If  
%>
         &nbsp; Go To Page

<select name="currentPage" onchange="MovePage.submit()">
<%
' Dynamically generate page numbers
      For i = 1 to totalPages
            If i = CInt(currentPage) Then y = "selected" else y = ""
            Response.Write "<OPTION " &y& " VALUE="&i&">" & i & "</OPTION>"
      Next
%>
</select>

Page <%=currentPage%> of <%=totalPages%>
</form>
0
Comment
Question by:yourbudweiser
  • 2
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
NickUpson earned 100 total points
ID: 16985447
"I am only getting the recordcount on the first run but this won't account for newly updated transactions" - actually you won't see the new updates/insert/deletes until the transaction making those changes does a commit and then your code does a commit. Unless you are connection with autocommit on, which would be unusual.

There is no way to combine the count of rows & the rows themselves unless you use a stored procedure something like this

CREATE PROCEDURE myproc(EMP_U INTEGER)
RETURNS (recs int)
AS

select count(*) from mytable where col1 = :emp_u into :recs;
suspend; /* returns 1 row */

FOR select emp_u from mytable where col1 = :emp_u into :recs
DO
BEGIN
 suspend; /* returns 1 row at a time */
end
end
0
 
LVL 3

Author Comment

by:yourbudweiser
ID: 16992554
is this the only solution you see to calculate how many records to skip? is there a way to put the recordcount into a variable and then use it in the same SQL statement?
0
 
LVL 19

Expert Comment

by:NickUpson
ID: 16993000
in the same sql statement - no, but the approach above will only require one statement to execute it - select * from myproc(34) and could equally well be passed the first and skip parameters as well
0
 
LVL 3

Author Comment

by:yourbudweiser
ID: 17003488
thanks for the help but I cannot use the SP, it would require modifying my web pages way too much :-)

I will examine and possibly use at another time.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Database tuning – How to start and what to tune. This question is frequently asked by many people, both online and offline. There is no hard and fast rule-of-thumb for performance tuning, however, before beginning the tuning process one should a…
Read about achieving the basic levels of HRIS security in the workplace.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now