Forum Moderators: open

Message Too Old, No Replies

cf to Asp

Sessions, whats with these ASP Sessions?

         

HeadBut

3:47 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



This code works when there is no session:
<%if Session("Bemms") = "" then%>

But as soon as I create a session it dies? What is with sessions in ASP?

error '80020009'
Exception occurred.

/WCABHeader.asp, line 27

================ here is the working/not working code:
<%if Session("Bemms") = "" then%><A href="SignIn.asp">Sign-In</a><% elseif Session("Access") = "Admin" then%>- Admin "On"<% end if %>

thanks!

txbakers

5:41 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



maybe because the session is null not ""?

try a cstr(Session("Bemms")) and see if that helps the situation.

or maybe an if isnull(Session("Bemms"))

Dreamquick

5:50 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's more likely that while
Session("Bemms")
is blank you're not running the problem code, but when it isn't blank you're running
Session("Access") = "Admin"
which is what you reported as generating this error the other day [webmasterworld.com]...

Since it's all on the one line it's hard to say if it's the "if" clause or the "elseif" that's the cause of the problem, breaking the statements onto different lines should probably be your first step.

If it is still the

Session("Access")
causing this problem then I'd suggest looking at how you're populating it ... off the top of my head I think I've seen similar problems when you store an object in a session and then try to compare that object to a string directly (but don't quote me on it).

- Tony

HeadBut

7:28 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



cstr(Session("Access")) = "Admin"
did not work for either session var.

isnull did work for the First session but now I need to check the contents of the second session var (the one above).

as for how they are getting set:
Set TrySignIn = MyConn.Execute(SQL_QUERY)
if TrySignIn("Access") <> "" then
Set Session("Access") = TrySignIn("Access")
Set Session("Bemms") = TrySignIn("Bemms")
end if

and I have tried:

Set Session("Access") = cstr(TrySignIn("Access"))

but it does not work.

Thanks - really Many Thanks!

Dreamquick

7:53 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's your problem!

Set TrySignIn = MyConn.Execute(SQL_QUERY)
if TrySignIn("Access") <> "" then
Set Session("Access") = TrySignIn("Access")
Set Session("Bemms") = TrySignIn("Bemms")
end if

Once you place an object into a variable you can't treat it like it's a string and expect it to work (even if it does work it's still a bad habit), that's probably what's generating your exception.

In this case your problem is that you're using SET to put the field object rather than then field contents into the session variable which is probably why it chokes when doing a string comparison.

So how do you fix this? Well the first problem is that you're using SET when it looks like you just want string data, so lose the SETs. The second problem two is that you're using the default property on the field array, it's safer to explicitly request the field value (.value) because then you know what you're getting.

That should leave you with something like;

Set TrySignIn = MyConn.Execute(SQL_QUERY)
if TrySignIn("Access").value <> "" And Not IsNull( TrySignIn("Access") ) then
Session("Access") = TrySignIn("Access").value
Session("Bemms") = TrySignIn("Bemms").value
end if

On the other hand if you did want to check if a variable contains an object then the way to do it is via

IsObject
rather than checking if the variable is not equal to a blank string.

- Tony

HeadBut

9:24 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



COOL!

When I set the session properly: ".value" then everything starts to work:
Check for Admin:
if Session("Access") = "Admin" then%>- Admin "On"<% end if %>

Check for not login:
if Session("Bemms") = "" then%>

perfect! many thanks!

a couple of tutorials and examples and I have converted an entire CF app in only a few days!