Negative Recordcount Value in asp

Discussion in 'C#' started by surfnearnzone, Mar 6, 2007.

  1. #1
    I struggled for last 24 hours to get this code straight and finally i have cracked it. So just thinking that other people don't get troubled by this error, i am writing it here.

    I was trying to get Recordcount for a set of records in my database table. The code which was causing problem was very straight and simple and correct !!!:

    
    
    <%
    Dim cn, rs, s_id, lnk_id, redir_to,dbpath
    
    dbpath = server.MapPath("testdb.mdb")
    
    Set cn = server.CreateObject("adodb.connection")
    
    set rs = server.createobject("adodb.recordset")
    
    rs.CursorType = adOpenkeyset
    
    cn.open "Provider=Microsoft.Jet.OLEDB.4.0;data source=" & dbpath
    
    rs.open "select * from [tblLinks]", cn
    
    
    dim r_count, ctr
    
    r_count = 0
    r_count = rs.recordcount
    ctr = 0
    
    rs.movefirst
    
    response.write(r_count)
    
    
    %>
    
    
    Code (markup):
    I almost lost my head trying to figure out the problem in the code. But was not able to get a positive Recordcount value.

    Although the code is very correct it is missing one line of code:

    Add this piece of code at the very beginning:
     <!-- #include file=adovbs.inc --> 
    Code (markup):
    This is most important piece of code to get any database driven code to work.

    Amazingly i found all kinds of advice but this one on the internet, increasing my frustration. So i had to add my two cents for the solution of the problem.

    May search engines crawl this thread and help other troubled coders with the same problem:D

    And yes, you must have the file ADOVBS.INC :)
     
    surfnearnzone, Mar 6, 2007 IP
  2. MarkusJ_NZ

    MarkusJ_NZ Well-Known Member

    Messages:
    240
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi, just a quick comment, the ADOVBS.INC file is a rather large file which basically aliases a few variables so you do not have to use the numerical equivalent. In your case you are loading a file just to be able to replace something like rs.CursorType = 1 with rs.CursorType = adOpenkeyset.

    See this link
    http://classicasp.aspfaq.com/general/should-i-use-adovbs-inc-for-declaring-constants.html

    Also, try to avoid using SELECT * in your sql statements. Try to explicitly retrieve just the columns you will actually use, it's way faster.

    Good on you for posting what you found back to the community to help people out :) :)

    Regards
    Markus
     
    MarkusJ_NZ, Mar 6, 2007 IP
  3. JenniP

    JenniP Peon

    Messages:
    250
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The negative recordcount is actually what you expect to get when you use certain ways of opening the record set different cursortypes have different properties.

    Basically if you get -1 it means that your database knows you have records in your record set but has not done a proper record count which makes it faster, so if you only need to read a recordset record by record and never go backwards and dont need to know a correct record count ForwardOnly is faster as the database just gets the records and doesnt do a full count.

    BOF and EOF still work correctly in these suituation.

    Jen
     
    JenniP, Mar 7, 2007 IP
  4. mickn

    mickn Peon

    Messages:
    589
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #4
    When you use Forward cursor you cannot retrieve the recordcount. That's it. Use another type of cursor and it's be fine. It's the same whether you use, asp, Visual Basic, Delphi... it's the way ADO works.
     
    mickn, Mar 11, 2007 IP
  5. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #5
    Argg.. makes me kringe every time I see Select *...
     
    ccoonen, Mar 12, 2007 IP