• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

ASP and ADO: Why is recordset.PageCount returning -1?

Why is recordset.PageCount returning -1? Given the ASP code below, the SQL returns serveral rows, however, when I try to use the result.RecordCount I always get -1. Why is this? And how can I get access to the size of the recordset 'result'?

Many Thanks

strSQL = "SELECT YEAR([Date]) As [Year], MONTH([Date]) As [Month], ROUND([Percent Return], 2), [Fund Name] FROM [Fund Perf] WHERE [Fund ID] = " & fundID & "ORDER BY [Date] asc"

      Set result = Server.CreateObject("ADODB.recordset")
      result.Open strSQL, connection
                             
      response.write(result.PageCount)
0
inghfs
Asked:
inghfs
1 Solution
 
ETA-TECHNICIANSCommented:
check your cursor

from: http://www.w3schools.com/ado/prop_rs_recordcount.asp

The RecordCount property returns a long value that indicates the number of records in a Recordset object.

If the Recordset object supports AbsolutePosition and AbsolutePage properties or bookmarks (if Supports(adApproxPosition) or Supports(adBookmark) returns true), this property will return the exact number of records in the Recordset.

Note: This property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and -1 or the actual count for a dynamic cursor.

Note: The Recordset object must be open when calling this property. If this property is not supported it will return -1.
0
 
deathtospamCommented:
inghfs --

ETA-TECHNICIANS is correct -- your cursor isn't set correctly, which is why RecordCount returns -1.  ASPFAQ.com does an excellent job of explaining this topic ( http://classicasp.aspfaq.com/general/why-does-recordcount-return-as-1.html ), but if all you want is an example, here it is:

=============================================
'// Set the recordset's CursorType property to "adOpenKeyset" (1).
'// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmthrstopen.asp
Set result = Server.CreateObject("ADODB.recordset")
result.Open strSQL, connection, 1
iRecordCount = result.RecordCount
=============================================


-= DeathToSpam =-
0
 
Anthony PerkinsCommented:
Let's set the record straight:

First of all the reason you are getting a RecordCount of -1 is because the provider does not support the RecordCount property with the cursor you are using.  There is good reason for this:  The provider is making IMHO the very valid assumption that you want the fastest result possible.  If you do not care how long it takes or how much resources are occupied, consider using some other cursor type / location that does support this property.  If you do care about performance and resources used, than there are far better ways of doing it without using the RecordCount property.  If fact I will go out on a limb and suggest that you can do all you need with a performant cursor and never use the RecordCount property.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now