Forum Moderators: open

Message Too Old, No Replies

IF statement not behaving as expected

         

nwhorton

9:55 pm on Sep 29, 2005 (gmt 0)

10+ Year Member



I'm writing some code that will delete an entry from a database when supplied with its ID number. I don't want to paste the whole code, but here is the if statement causing problems:


While Not objRec.EOF
If (objRec("ID") = ID) THEN
response.Write("Success. ID = " & objRec("ID"))
varBookmark = objRec.Bookmark
ELSE
response.Write("Fail<br>")
End If
objRec.MoveNext
Wend

Whenever I call this script and it gets to this statement, it goes to the FAIL (else) case every time. If I do a response.write(objRec("ID")) in each iteration of the while loop, the expected value comes up for each entry, and when I check the value of ID it is also the expected value, however the if statement never recognizes them as equal. Does anyone have any clue what's going on here?

An example is if I pass in the ID equal to 8, the while loop goes through all the entries 1,2,...,8,...,etc and fails every time, even when it gets to 8. Both variables are printed to the screen and read "8" but the if statement doesn't recognize them as equal.

john_k

10:33 pm on Sep 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your values have different datatypes. Most likely one is a string. Alter your If statement to something like this:


If (CLng(objRec("ID")) = CLng(ID)) Then

If you determine that the problem is that ID is a string, then you could make it a little more efficient by doing the CLng conversion on ID prior to the loop.

Also, if that is an ADO recordset, then it is also a good idea to explicitely use the Value property:


If (CLng(objRec("ID").Value) = CLng(ID)) Then

nwhorton

5:45 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



Thanks a lot...the CLng function worked. I had a hunch that the datatypes weren't going to be the same, but I wasn't sure how to go about checking/fixing this. Thanks for the hint.