Go Back   CodingForums.com > :: Server side development > ASP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-24-2004, 08:54 PM   PM User | #1
Svenson
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Svenson is an unknown quantity at this point
If problem

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
Svenson is offline   Reply With Quote
Old 08-24-2004, 09:21 PM   PM User | #2
fractalvibes
Regular Coder

 
Join Date: Aug 2002
Location: Texas
Posts: 287
Thanks: 0
Thanked 0 Times in 0 Posts
fractalvibes is an unknown quantity at this point
aren't you missing the end ifs from those statements in question?
fractalvibes is offline   Reply With Quote
Old 08-24-2004, 09:49 PM   PM User | #3
Morgoth
Senior Coder

 
Morgoth's Avatar
 
Join Date: Jun 2002
Location: Ontario, Canada Remaining Brain Cells: 6
Posts: 1,402
Thanks: 2
Thanked 1 Time in 1 Post
Morgoth is an unknown quantity at this point
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:
Code:
strSport = Int(Request.QueryString("sport"))
If strSport <> 0 Then
  whichsportfield = strSport
Else
  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.


Quote:
Originally Posted by fractalvibes
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
Example:
Code:
If 1 = 1 Then Response.Write "hi" 'WORKS
Code:
If 1 = 1 Then Response.Write "hi" Response.Write "<hr>" 'DOESN'T WORK
Code:
If 1 = 1 Then Response.Write "hi": Response.Write "<hr>" 'WORKS
Code:
If 1 = 1 Then Response.Write "hi"
Response.Write "<hr>" 'WORKS
Code:
If 1 = 1 Then
Response.Write "hi"
Response.Write "<hr>" 'DOESN'T WORK
That help, fractalvibes?

Last edited by Morgoth; 08-24-2004 at 09:52 PM.. Reason: Added Integer explaination.
Morgoth is offline   Reply With Quote
Old 08-24-2004, 10:05 PM   PM User | #4
fractalvibes
Regular Coder

 
Join Date: Aug 2002
Location: Texas
Posts: 287
Thanks: 0
Thanked 0 Times in 0 Posts
fractalvibes is an unknown quantity at this point
Yep, but that is not good practice (sloppy!)
fractalvibes is offline   Reply With Quote
Old 08-24-2004, 10:15 PM   PM User | #5
Svenson
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Svenson is an unknown quantity at this point
Morgoth
Thanks a lot.
That would explain about 50 other problems I've had too. :-P
-Brian
Svenson is offline   Reply With Quote
Old 08-24-2004, 10:23 PM   PM User | #6
Morgoth
Senior Coder

 
Morgoth's Avatar
 
Join Date: Jun 2002
Location: Ontario, Canada Remaining Brain Cells: 6
Posts: 1,402
Thanks: 2
Thanked 1 Time in 1 Post
Morgoth is an unknown quantity at this point
Quote:
Originally Posted by fractalvibes
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.

Quote:
Originally Posted by Svenson
Morgoth
Thanks a lot.
That would explain about 50 other problems I've had too. :-P
-Brian
No problem. It's something everyone who works with ASP should know.
Morgoth is offline   Reply With Quote
Old 08-26-2004, 01:32 PM   PM User | #7
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Quote:
Originally Posted by Morgoth
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
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 08-26-2004, 09:59 PM   PM User | #8
Svenson
New to the CF scene

 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Svenson is an unknown quantity at this point
Nice.
Makes sense and doesn't at the same time.
It works though, so thank you.
-Brian
Svenson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:34 PM.


Advertisement
Log in to turn off these ads.