View Full Version : If problem

08-24-2004, 08:54 PM
Hi all,
First of all I'm sure that this is probably a very basic problem, but I am at a very basic skill level with asp.
I really appreciate any help anyone can provide.

OK, so the line I am having an issue with is this:

If rs.Fields("Sport1") = Request.QueryString("sport") Then whichsportfield = 1
If rs.Fields("Sport2") = Request.QueryString("sport") Then whichsportfield = 2
If rs.Fields("Sport3") = Request.QueryString("sport") Then whichsportfield = 3

I end up with whichsportfield equaling nothing.
if I put "whichsportfield = 1" after that all of the code following that functions fine, so I know my problem is in those statements.
Now it looks to me like everything in there should work, as earlier on I use the following line with no problem:

sql = "SELECT * FROM [Students] WHERE Sport1 = " & Request.QueryString("sport")
sql = sql & " OR Sport2 = " & Request.QueryString("sport")
sql = sql & " OR Sport3 = " & Request.QueryString("sport")

So I know the functions I'm trying to use should work, yes? Is there some kind of problem with the format of my If statement or something?

I also tried adding:

response.write request.querystring("sport")
response.write rs.Fields("Sport1").Value

and both of them are "1"
so I don't see why they shouldn't match.

Sorry if that's not enough info. Just tell me what you need and I'll paste more.

Thanks a lot,
Brian Hill

08-24-2004, 09:21 PM
aren't you missing the end ifs from those statements in question?

08-24-2004, 09:49 PM
Svenson, you're going about your method in a strange way. I would suggest first gathering the QueryString, and putting it in a variable.
strSport = Request.QueryString("sport")

Now, it seems like your Request.QueryString("sport") is going to be an Integer (a number) from 1 to 3, correct? I will assume it will be a number in the following code. So, why not remove the If statements are just write:

strSport = Int(Request.QueryString("sport"))
If strSport <> 0 Then
whichsportfield = strSport
Response.Write "ERROR"
End If

Svenson, the reason why your script did not work is because when you take an Integer (a number) out of a QueryString (page.asp?Id=#) it changes the number into a String type. So you change 1, 2, or 3, into a integer, with the Int() function, so it can match the integer in the database with the integer submited by the user. Understand? Reply back if you don't.

aren't you missing the end ifs from those statements in question?

You need an End If if you don't put anything after Then

If 1 = 1 Then Response.Write "hi" 'WORKS

If 1 = 1 Then Response.Write "hi" Response.Write "<hr>" 'DOESN'T WORK

If 1 = 1 Then Response.Write "hi": Response.Write "<hr>" 'WORKS

If 1 = 1 Then Response.Write "hi"
Response.Write "<hr>" 'WORKS

If 1 = 1 Then
Response.Write "hi"
Response.Write "<hr>" 'DOESN'T WORK

That help, fractalvibes?

08-24-2004, 10:05 PM
Yep, but that is not good practice (sloppy!)

08-24-2004, 10:15 PM
Morgoth :D
Thanks a lot.
That would explain about 50 other problems I've had too. :-P

08-24-2004, 10:23 PM
Yep, but that is not good practice (sloppy!)

Not necessarily is it sloppy or bad practice, it's very good for keeping single answers small. I personally aviod using If statements that way, but it's good to know they exist.

Morgoth :D
Thanks a lot.
That would explain about 50 other problems I've had too. :-P

No problem. It's something everyone who works with ASP should know.

08-26-2004, 01:32 PM
strSport = Int(Request.QueryString("sport"))

That would produce a type mismatch error if "sport" is not a number. Since "sport" is a querystring, the user can easily change its value and your page will not be able to handle the error. You should check first if its numeric by using the IsNumeric() function.

But the real problem is not actually the sport querystring not being an integer. As in any loosely-typed languages like VBScript, comparing 1="1" will result to True. The same is true for Javascript (you have to use strict equality operator === to make strict comparison)

I remember I also experienced that weird error for comparing recordset field values to a variable or expression even though outputting them will result to same values. The solution is to store first the recordset field value to a variable before doing the comparison. I forgot the exact reason behind that.

dim qsSport, rsSport1, rsSport2, rsSport3, whichsportfield
qsSport = Request.Querystring("sport")
rsSport1 = rs.Fields("Sport1")
rsSport2 = rs.Fields("Sport2")
rsSport3 = rs.Fields("Sport3")
if rsSport1=qsSport then whichsportfield = 1
if rsSport2=qsSport then whichsportfield = 2
if rsSport3=qsSport then whichsportfield = 3

08-26-2004, 09:59 PM
Makes sense and doesn't at the same time. :D
It works though, so thank you.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum