-2

Im working on some classic ASP functions.

In one wsc file im setting the session like so.

Session("ordertype") = "morning"

Then in the other wsc file im checking if that exists like so.

If Session("ordertype") Is Nothing Then
    ' Do stuff here
End if

But everytime it gets to checking if the session exists, the application just stops.

What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
BigJobbies
  • 3,633
  • 11
  • 43
  • 66

1 Answers1

2

The Is operator is valid but not in the context you are trying to use it.

From the VbScript Reference

Compares two object reference variables.

result = object1 Is object2

If you where setting an object reference using the Set command then using this kind of comparison would be acceptable.

In this case though your Session("ordertype") contains a string which is not an object reference type, instead use a simple Len() check to check whether you have assigned a string or not. To avoid Nulls use

'Check Length of Session value avoid Null by concatenating empty string.
If Len(Session("ordertype") & "") > 0 Then
  'Do Stuff here
End If

Alternative Method

You can also use VarType(Session("ordertype")) to check your Session variable type first to avoid Nulls as @ZeeTee suggests.

Community
  • 1
  • 1
user692942
  • 16,398
  • 7
  • 76
  • 175
  • @ZeeTee Where's this db object? `Session("ordertype") = "morning"` That my friend is a `string` nothing more. – user692942 Sep 12 '14 at 16:35
  • @ZeeTee What are you on about? Just because they have written `If Session("ordertype") Is Nothing Then` doesn't make it right. The value is in the code sample `Session("ordertype") = "morning"` which is a string, I'm lost to where this *"db object"* reference you have plucked out of the air is coming from. The OP doesn't mention dbs, connections or anything like that, it is all assumption on your part. – user692942 Sep 12 '14 at 16:41
  • Ok, then you can't use `Is Nothing` is the answer since the variable isn't an object. If you make an edit I will remove my downvote. – Control Freak Sep 12 '14 at 16:42