#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    365
    Rep Power
    11

    how to check if a variable exists or isset in asp?


    how can i check to see if a variable exists? or whether it has been set in asp?
    thanks
  2. #2
  3. http://www.softdevelop.ru
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    298
    Rep Power
    10
    try to use:

    if not variable = null then ....
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    365
    Rep Power
    11
    i tried:

    if NOT isnull(variable) then

    but it doesnt work
  6. #4
  7. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,424
    Rep Power
    4539
    I don't think there is a similar VBScript function to isset, at least not one that comes to mind. I generally use Option Explicit in asp code so there aren't any undefined variables in code.
    ======
    Doug G
    ======
    The man who doesn't read good books has no advantage over the man who can't read them.
    --Mark Twain
  8. #5
  9. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2004
    Location
    Tucson, Sonora
    Posts
    1,371
    Rep Power
    51
    In ASP, I typically use Len(variable)>2, or any number safe enough to make assumptions on. Typically, if a recordset or form variable is undefined, this will return 0, where in PHP, an error is thrown, thus the need for isset.

    Hope that helps.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    365
    Rep Power
    11
    so try len(var)>0

    what ive got working is to find whether a form has been posted
    in php i have used:
    isset($_POST)
    and
    $_SERVER['REQUEST_METHOD'] == "post"

    but i also use isset alot in php, to check if a variable has been set, if not then either create it or do something

    what will Option Explicit do if i used it?
    the: NOT isnull(var) option doesnt seem to work as an asp alternative to isset but could this be due to not setting Option Explicit?
  12. #7
  13. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2004
    Location
    Tucson, Sonora
    Posts
    1,371
    Rep Power
    51
    I've had no luck with the isnull function in the past. ASP seems to have strange ideas about the length and type of a variable pulled from a database.

    Option Explicit requires that all variables be defined before they are called by using Dim. Normally ASP does not require this, and many lazy coders (me included) don't use it so that they don't have to write out every variable that they're going to use in the page.
    Info on Option Explicit

    To check whether a form has been posted, you can either do
    If Len(Request.Form("fieldName")>1 Then
    as long as that fieldName corresponds to a field you are certain will be populated.
    Or, add a senseless URL variable to the form's action, like:
    <form action="post.asp?submit=1">
    Then:
    If Request.QueryString("submit")=1 Then

    Hope that helps.
  14. #8
  15. http://www.softdevelop.ru
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    298
    Rep Power
    10
    if Request.ServerVariables("REQUEST_METHOD") = "POST" then
    if not Request.Form("control_name") = "" then
    ..........
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    371
    Rep Power
    11
    I think ISEMPTY can be used

    Cut and paste from the quote in ur ASP page and check what is the result
    dim vstr
    vstr="Hello"

    if isempty(vstr) then
    Response.write "<br>V is empty <br>"
    else
    Response.write "<br>V is not empty <br>"
    end if

    //---vstr1 is not declared and assigned
    if isempty(vstr1) then
    Response.write "<br>V1 is empty <br>"
    else
    Response.write "<br>V1 is not empty <br>"
    end if
    ISEMPTY can be used as follows
    isEmpty(Request.Form("control_name")) for the existance of a single field

    isempty(Request.Form) can be used to check whether the form has been submitted or not
    Last edited by brightlight; November 9th, 2004 at 10:34 PM.
    Regards,
    Brightlight
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    1
    Rep Power
    0
    I'm having the same problem with an Isset or similar function.

    Basically I'm trying to access a database, and if the select comes back with nothing then print one thing, otherwise print something else....

    So Was there any conclusion to this thread then?
  20. #11
  21. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2004
    Location
    Tucson, Sonora
    Posts
    1,371
    Rep Power
    51
    I usually use this:

    If Len(yourRS("yourField"))>1 Then
    ....DO SOMETHING....
    Else
    ....DO SOMETHING ELSE....
    End If

    Does that not work?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Try this


    You can try use this code:

    If TypeName(var) <> "Nothing" Then
    ....DO SOMETHING....
    Else
    ....DO SOMETHING ELSE....
    End If

    Comments on this post

    • requinix disagrees : might have been helpful... back in 2004
  24. #13
  25. No Profile Picture
    Grumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,424
    Rep Power
    4539
    Originally Posted by rtorresbr
    You can try use this code:

    If TypeName(var) <> "Nothing" Then
    ....DO SOMETHING....
    Else
    ....DO SOMETHING ELSE....
    End If
    Thanks for the reply, but you may not have noticed this is a very old thread Also, I think the Nothing keyword only applies to object variables.
    ======
    Doug G
    ======
    The man who doesn't read good books has no advantage over the man who can't read them.
    --Mark Twain

IMN logo majestic logo threadwatch logo seochat tools logo