473,388 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Very strange behavior of "If Not rs.EOF", Need help...

I can't get my head around this!

I have the following code:

<%
.... Code for connection to the database ...
.... Code for retrieving recordset ...

If Not rs.EOF Then
... Do something...
End If
%>

Very basic. Very simple. Except for the fact the the conditional is
completely ignored when I run the script!! <_<

That's right... I deliberately enter a non existant value in my SQL
query:

<%
sql = "Select * From user_database Where username='non-existant-name'"
rs.Open sql, conn

If Not rs.EOF Then
... Do something...
End If
%>

In this case rs.EOF should be set to true, since the username that I
entered does not exist! Therefor the condition of Not rs.EOF is not
met, and the code inside the conditional should not run, however for
some strange reason it does run... so what's going on here?!

I noticed that if I write it differently it does work like it's
supposed to:

<%
If rs.EOF Then
Response.Write("The record does not exist.<br />")
Else
... Do Something ...
End If
%>

However it should work the other way too, so this is very strange to
say the least! Does anybody have any idea as to what is going on here?
Is there something wrong with my code? Because I noticed that when I
use the 'Else' clause it does work... but since when is it a problem
writing a conditional without an 'Else' clause?

Thanks!

Aug 22 '06 #1
12 20583
i always use...

with rs
if not(.bof and .eof) then
In article <11*********************@b28g2000cwb.googlegroups. com>,
qu********@gmail.com says...
>
I can't get my head around this!

I have the following code:

<%
... Code for connection to the database ...
... Code for retrieving recordset ...

If Not rs.EOF Then
... Do something...
End If
%>

Very basic. Very simple. Except for the fact the the conditional is
completely ignored when I run the script!! <_<

That's right... I deliberately enter a non existant value in my SQL
query:

<%
sql = "Select * From user_database Where username='non-existant-name'"
rs.Open sql, conn

If Not rs.EOF Then
... Do something...
End If
%>

In this case rs.EOF should be set to true, since the username that I
entered does not exist! Therefor the condition of Not rs.EOF is not
met, and the code inside the conditional should not run, however for
some strange reason it does run... so what's going on here?!

I noticed that if I write it differently it does work like it's
supposed to:

<%
If rs.EOF Then
Response.Write("The record does not exist.<br />")
Else
... Do Something ...
End If
%>

However it should work the other way too, so this is very strange to
say the least! Does anybody have any idea as to what is going on here?
Is there something wrong with my code? Because I noticed that when I
use the 'Else' clause it does work... but since when is it a problem
writing a conditional without an 'Else' clause?

Thanks!
Aug 23 '06 #2
So you're wasting keystrokes. how is that relevant to the user's problem?

fantum wrote:
i always use...

with rs
if not(.bof and .eof) then
In article <11*********************@b28g2000cwb.googlegroups. com>,
qu********@gmail.com says...
>>
I can't get my head around this!

I have the following code:

<%
... Code for connection to the database ...
... Code for retrieving recordset ...

If Not rs.EOF Then
... Do something...
End If
%>

Very basic. Very simple. Except for the fact the the conditional is
completely ignored when I run the script!! <_<

That's right... I deliberately enter a non existant value in my SQL
query:

<%
sql = "Select * From user_database Where
username='non-existant-name'" rs.Open sql, conn

If Not rs.EOF Then
... Do something...
End If
%>

In this case rs.EOF should be set to true, since the username that I
entered does not exist! Therefor the condition of Not rs.EOF is not
met, and the code inside the conditional should not run, however for
some strange reason it does run... so what's going on here?!

I noticed that if I write it differently it does work like it's
supposed to:

<%
If rs.EOF Then
Response.Write("The record does not exist.<br />")
Else
... Do Something ...
End If
%>

However it should work the other way too, so this is very strange to
say the least! Does anybody have any idea as to what is going on
here? Is there something wrong with my code? Because I noticed that
when I use the 'Else' clause it does work... but since when is it a
problem writing a conditional without an 'Else' clause?

Thanks!
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Aug 23 '06 #3
John wrote:
I can't get my head around this!

I have the following code:

<%
... Code for connection to the database ...
... Code for retrieving recordset ...

If Not rs.EOF Then
... Do something...
End If
%>

Very basic. Very simple. Except for the fact the the conditional is
completely ignored when I run the script!! <_<

That's right... I deliberately enter a non existant value in my SQL
query:

<%
sql = "Select * From user_database Where username='non-existant-name'"
rs.Open sql, conn

If Not rs.EOF Then
... Do something...
End If
%>

In this case rs.EOF should be set to true, since the username that I
entered does not exist! Therefor the condition of Not rs.EOF is not
met, and the code inside the conditional should not run, however for
some strange reason it does run... so what's going on here?!

I noticed that if I write it differently it does work like it's
supposed to:

<%
If rs.EOF Then
Response.Write("The record does not exist.<br />")
Else
... Do Something ...
End If
%>

However it should work the other way too, so this is very strange to
say the least! Does anybody have any idea as to what is going on here?
Is there something wrong with my code? Because I noticed that when I
use the 'Else' clause it does work... but since when is it a problem
writing a conditional without an 'Else' clause?
It's puzzling to me as well. Could you whip up an example using the pubs or
Northwind database so we could try the code ourselves? I suspect the root
cause may be found in the stuff you snipped:

<%
.... Code for connection to the database ...
.... Code for retrieving recordset ...

Give us a more complete example so we can see the behavior for ourselves.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Aug 23 '06 #4
On Wed, 23 Aug 2006 06:30:48 -0500, Bob Barrows [MVP]
<re******@NOyahoo.SPAMcomwrote:
John wrote:
>I can't get my head around this!

I have the following code:

<%
... Code for connection to the database ...
... Code for retrieving recordset ...

If Not rs.EOF Then
... Do something...
End If
%>

Very basic. Very simple. Except for the fact the the conditional is
completely ignored when I run the script!! <_<

It's puzzling to me as well. Could you whip up an example using the pubs
or
Northwind database so we could try the code ourselves? I suspect the root
cause may be found in the stuff you snipped:
If rs were Empty, or similarly invalid, On Error Resume Next would resume
inside the If block.

Option Explicit

Dim rs
On Error Resume Next
If Not rs.Eof Then
WScript.Echo "resumed here"
End If

Considering everybody somehow seems to know about that thing without
actually knowing what it _does_, my money's on that. :)

--
Justin Piper
Bizco Technologies
http://www.bizco.com/
Aug 23 '06 #5
Yeah, alright.. so you guys want a more complete code, here it is:

<%
Set conn = Server.CreateObject("ADODB.Connection")
con_str = "Provider=sqloledb;Data Source=[ My Sever's Address
],1433;Initial Catalog=[ Database Name ];User Id=[ db Username
];Password=[ db Password ]"
conn.ConnectionString = con_str
conn.Open

Set rs = Server.CreateObject("ADODB.Recordset")
sql = "Select * From user_database Where username=" &
LCase(Request.Form("username")) ' Non existant username supplied in the
form

On Error Resume Next
rs.Open sql, conn

If Not rs.EOF Then
Response.Write("The record does not exist.<br />")
' Some more code here...
End If

rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>

1) When I remove the "On Error Resume Next", I do in fact get an error
message saying that the record doesn't exist.

2) I tried the following code just after the "rs.Open sql, conn" to see
the result:
<%
If rs.EOF Then Response.Write("rs.EOF = True") Else
Response.Write("rs.EOF = False")
%>

Worked like a charm.

But like this:
<%
If Not rs.EOF Then

' Some code here...

End If
%>

It totally ignored the fact that rs.EOF it True, and goes on to execute
the code anyway...
Anyone has anything?

Aug 23 '06 #6
John wrote:
Yeah, alright.. so you guys want a more complete code, here it is:

<%
Set conn = Server.CreateObject("ADODB.Connection")
con_str = "Provider=sqloledb;Data Source=[ My Sever's Address
],1433;Initial Catalog=[ Database Name ];User Id=[ db Username
];Password=[ db Password ]"
conn.ConnectionString = con_str
conn.Open

Set rs = Server.CreateObject("ADODB.Recordset")
sql = "Select * From user_database Where username=" &
LCase(Request.Form("username")) ' Non existant username supplied in
the form
We cannot test this since we don't have a database or table with that
name. I'm going to try to test it against the pubs database. I will
reply later on today with the result. In the meantime, read on for my
speculations:

As Justin pointed out (and I suspected), I believe the problem is in
this line:
On Error Resume Next
Comment it out and see if you get an error.
rs.Open sql, conn

If Not rs.EOF Then
If checking EOF causes an error, "Resume Next" causes this line to be
executed:
Response.Write("The record does not exist.<br />")
' Some more code here...
End If

rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>

1) When I remove the "On Error Resume Next", I do in fact get an error
message saying that the record doesn't exist.
Are you sure? I am unaware of an error message saying that a record does
not exist. What's the exact text of the error message? And which line of
code throws the error??
>
2) I tried the following code just after the "rs.Open sql, conn" to
see the result:
<%
If rs.EOF Then Response.Write("rs.EOF = True") Else
Response.Write("rs.EOF = False")
If this is a single line, then it really should not be legal. It was an
oversight that the vbscript parser fails to generate an error for this
statement. In VB, this would generate an error (single-line if
statements are not supposed to be able to contain Else clauses - the
vbscript developers goofed here.).
%>

Worked like a charm.
What was the result? Did "False" get written to Response? That would
mean that your code is really on two lines as shown.

It's a single-statement IF statement (I think) so the next statement
follows the Response.Write("rs.EOF = False").
>
But like this:
<%
If Not rs.EOF Then

' Some code here...

End If
%>

It totally ignored the fact that rs.EOF it True, and goes on to
execute the code anyway...
Anyone has anything?
Yes. see above. Checking EOF is causing an error which, because of the
"on error resume next" is causing the following line to be executed.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Aug 23 '06 #7
On Wed, 23 Aug 2006 10:03:48 -0500, John <qu********@gmail.comwrote:
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "Select * From user_database Where username=" &
LCase(Request.Form("username")) ' Non existant username supplied in the
form
On Error Resume Next
rs.Open sql, conn
You aren't quoting the username. So rs.Open fails, rs.EOF raises an error
(the recordset has to be open for EOF to be valid), and On Error Resume
Next resumes inside the block. Try this instead:

Set cmd = CreateObject("ADODB.Command")
With cmd
Set .ActiveConnection = conn
Set .CommandType = adCmdText
.CommandText = "Select * From user_database Where username=?"
Set rs = .Execute(,Array(LCase(Request.Form("username"))))
End With
2) I tried the following code just after the "rs.Open sql, conn" to see
the result:
<%
If rs.EOF Then Response.Write("rs.EOF = True") Else
Response.Write("rs.EOF = False")
%>
Worked like a charm.
But like this:
<%
If Not rs.EOF Then
' Some code here...
End If
%>
It totally ignored the fact that rs.EOF it True, and goes on to execute
the code anyway...
Like I said, when rs.EOF raises an error, On Error Resume Next will resume
in the If block. With this in mind:

If rs.EOF Then Response.Write "rs.EOF = True"

will appear to give the correct result, while:

If Not rs.EOF Then Response.Write "rs.EOF = False"

will not. The test was actually ignored in both cases, and execution
simply resumed inside the If block.

--
Justin Piper
Bizco Technologies
http://www.bizco.com/
Aug 23 '06 #8
From the last message I'm not sure what is the current situation...

As Justin said remove on error resume next if not already done (IMO should
be anyway avoided). What is the exact message you have ? (it's not clear if
this your own "record doesn't exist" message or some other one).

My first thought would have been an incorrect SQL statement...
--
Patrice

"John" <qu********@gmail.coma écrit dans le message de news:
11**********************@b28g2000cwb.googlegroups. com...
Yeah, alright.. so you guys want a more complete code, here it is:

<%
Set conn = Server.CreateObject("ADODB.Connection")
con_str = "Provider=sqloledb;Data Source=[ My Sever's Address
],1433;Initial Catalog=[ Database Name ];User Id=[ db Username
];Password=[ db Password ]"
conn.ConnectionString = con_str
conn.Open

Set rs = Server.CreateObject("ADODB.Recordset")
sql = "Select * From user_database Where username=" &
LCase(Request.Form("username")) ' Non existant username supplied in the
form

On Error Resume Next
rs.Open sql, conn

If Not rs.EOF Then
Response.Write("The record does not exist.<br />")
' Some more code here...
End If

rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>

1) When I remove the "On Error Resume Next", I do in fact get an error
message saying that the record doesn't exist.

2) I tried the following code just after the "rs.Open sql, conn" to see
the result:
<%
If rs.EOF Then Response.Write("rs.EOF = True") Else
Response.Write("rs.EOF = False")
%>

Worked like a charm.

But like this:
<%
If Not rs.EOF Then

' Some code here...

End If
%>

It totally ignored the fact that rs.EOF it True, and goes on to execute
the code anyway...
Anyone has anything?

Aug 23 '06 #9
John wrote:
Yeah, alright.. so you guys want a more complete code, here it is:

<%
Set conn = Server.CreateObject("ADODB.Connection")
con_str = "Provider=sqloledb;Data Source=[ My Sever's Address
],1433;Initial Catalog=[ Database Name ];User Id=[ db Username
];Password=[ db Password ]"
conn.ConnectionString = con_str
conn.Open

Set rs = Server.CreateObject("ADODB.Recordset")
sql = "Select * From user_database Where username=" &
LCase(Request.Form("username")) ' Non existant username supplied in
the form

On Error Resume Next
rs.Open sql, conn

If Not rs.EOF Then
Wait a second. This should be:

If rs.EOF then
Response.Write("The record does not exist.<br />")
' Some more code here...
End If
If EOF is true, then there are no records. Not the other way aroung

My initial tests show this working as expected, but I'm not getting an
error when opening the recordset or checking EOF. You really need to
address that error message.

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Aug 23 '06 #10
Bob Barrows [MVP] wrote:
John wrote:
My initial tests show this working as expected, but I'm not getting an
error when opening the recordset or checking EOF. You really need to
address that error message.
FYI, here is the code I used to test:

<%
Set conn = Server.CreateObject("ADODB.Connection")
con_str = "Provider=sqloledb;Data Source=clnsqldev7;" & _
"Initial Catalog=pubs;User Id=xxxxxxxx;Password=xxxxxx"
conn.ConnectionString = con_str
conn.Open

Set rs = Server.CreateObject("ADODB.Recordset")
sql = "Select * From authors Where au_fname='me'"

' Non existant username supplied in the form

On Error Resume Next
rs.Open sql, conn

If rs.EOF Then
Response.Write("The record does not exist.<br />")
' Some more code here...
End If

rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Aug 23 '06 #11
Justin Piper wrote:
You aren't quoting the username. So rs.Open fails, rs.EOF raises an error
(the recordset has to be open for EOF to be valid), and On Error Resume
Next resumes inside the block. Try this instead:

Oh my god! I can't believe I missed that... This is so embarrassing you
guys!
I guess I must have been really tired when I wrote that code... head
wasn't working!

Yes, that was indeed the problem! After I quoted the name everything
started working as it should again...

Thanks a lot for all your time, guys.
Sorry to have bothered you with such a stupid mistake! :)

John

Aug 23 '06 #12
John wrote:
Justin Piper wrote:
>You aren't quoting the username. So rs.Open fails, rs.EOF raises an
error (the recordset has to be open for EOF to be valid), and On
Error Resume Next resumes inside the block. Try this instead:


Oh my god! I can't believe I missed that... This is so embarrassing
you guys!
I guess I must have been really tired when I wrote that code... head
wasn't working!

Yes, that was indeed the problem! After I quoted the name everything
started working as it should again...

Thanks a lot for all your time, guys.
Sorry to have bothered you with such a stupid mistake! :)
Are you interested in a technique to help you avoid that mistake in the
future? This technique not only relieves you of the necessity to ever
worry about delimiters again, it also decisively defeats any sql
injection attempts:

parameter markers:
http://groups-beta.google.com/group/...e36562fee7804e

--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Aug 23 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
12
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
13
by: Jim in Arizona | last post by:
I made a page with a gridview that has rows show a different color if a number in a column is greater than or equal to 45. I also did this conditional formatting for the column next to it. Here's...
2
by: marsarden | last post by:
write code like: int main(void) { int a=10; if(a<20) {} } Compiler ok on dev-cpp . don't we have to add a ";" after if
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
9
by: chutsu | last post by:
hi I got a simple program, and I was wondering how do you check if the string in an array = a string. For example if I put "APPLE" in array Array then how can I check it with a if statement. if...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.