BOF-EOF

R

rn5a

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?
 
D

Daniel Crichton

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top