BOF-EOF

Discussion in 'ASP General' started by rn5a@rediffmail.com, Apr 12, 2007.

  1. Guest

    Consider the following code which retrieves records from a MS-Access
    database table:

    <%
    Dim objConn
    Set objConn=Server.CreateObject("ADODB.CONNECTION")
    'open connection using ConnectionString

    Dim strSQL1
    strSQL1="SELECT......"

    Dim objRS1
    Set objRS1=Server.CreateObject("ADODB.RECORDSET")
    objRS1.Open strSQL1,objConn,adOpenKeyset

    If Not(objRS1.EOF)
    Response.Write(objRS1("Col1"))
    Else
    Dim strSQL2
    strSQL2="SELECT....."

    Dim objRS2
    Set objRS2=Server.CreateObject("ADODB.RECORDSET")
    objRS2.Open strSQL2,objConn

    Response.Write(objRS2("Col1"))

    objRS2.Close
    Set objRS2=Nothing
    End If

    Response.Write(objRS1("Col2"))
    %>

    Note that both the SQL queries will retrieve only ONE record. Now when
    I execute the above code, ASP generates the

    Exception occured.

    error without pointing to any line. What could be the cause of the
    error? I guess it could be because of the last Response.Write line
    which spits out the value of objRS1("Col2") but I don't understand
    what could be the reason for the last Response.Write line to generate
    the above error.

    If I add the following line

    objRS1.MoveFirst

    immediately after the End If line & just before the last
    Response.Write line, then the error changes to

    Either BOF or EOF is True, or the current record has been deleted.
    Requested operation requires a current record.

    pointing to the objRS1.MoveFirst line. Can someone please point out
    what's wrong with the above code?
     
    , Apr 12, 2007
    #1
    1. Advertising

  2. wrote on 11 Apr 2007 17:14:41 -0700:

    > Consider the following code which retrieves records from a MS-Access
    > database table:
    >
    > <%
    > Dim objConn
    > Set objConn=Server.CreateObject("ADODB.CONNECTION")
    > 'open connection using ConnectionString
    >
    > Dim strSQL1
    > strSQL1="SELECT......"
    >
    > Dim objRS1
    > Set objRS1=Server.CreateObject("ADODB.RECORDSET")
    > objRS1.Open strSQL1,objConn,adOpenKeyset
    >
    > If Not(objRS1.EOF)
    > Response.Write(objRS1("Col1"))
    > Else
    > Dim strSQL2
    > strSQL2="SELECT....."
    >
    > Dim objRS2
    > Set objRS2=Server.CreateObject("ADODB.RECORDSET")
    > objRS2.Open strSQL2,objConn
    >
    > Response.Write(objRS2("Col1"))
    >
    > objRS2.Close
    > Set objRS2=Nothing
    > End If



    The following line will cause an error is objRS1 is empty

    > Response.Write(objRS1("Col2"))
    > %>


    > pointing to the objRS1.MoveFirst line. Can someone please point out
    > what's wrong with the above code?



    Hopefully it'll be obvious now why it errors. Also, you should check objRS2
    isn't empty before writing it's Col1 value too.

    Dan
     
    Daniel Crichton, Apr 12, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Muenchbourg
    Replies:
    0
    Views:
    274
    J. Muenchbourg
    Jul 11, 2003
  2. Christo

    BOF or EOF is true?

    Christo, Nov 13, 2006, in forum: ASP General
    Replies:
    7
    Views:
    215
    Mike Brind
    Nov 15, 2006
  3. .Net Sports
    Replies:
    0
    Views:
    210
    .Net Sports
    Jan 23, 2008
  4. GTN170777
    Replies:
    2
    Views:
    170
    Jeff Dillon
    Feb 28, 2008
  5. Yaara Mac

    Help with recordset.bof / eof

    Yaara Mac, Feb 5, 2008, in forum: Javascript
    Replies:
    3
    Views:
    110
    Evertjan.
    Feb 5, 2008
Loading...

Share This Page