Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: If problem

  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    Texas
    Posts
    287
    Thanks
    0
    Thanked 0 Times in 0 Posts
    aren't you missing the end ifs from those statements in question?

  • #3
    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
    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?

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    Texas
    Posts
    287
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yep, but that is not good practice (sloppy!)

  • #5
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Morgoth
    Thanks a lot.
    That would explain about 50 other problems I've had too. :-P
    -Brian

  • #6
    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
    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.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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

  • #8
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nice.
    Makes sense and doesn't at the same time.
    It works though, so thank you.
    -Brian


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •