Tuesday, March 13, 2012

Session Variables arent retrieved during Postback

Hi I have a web page, which has a login corner. When someone logs in and clicks the "Go" button, a class called Security gathers the users details and posts it back to the same page, under a session variable called "User"

Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
'Login into the system
Dim l As New Security()
Dim Outcome As PPPL.Security.LoginOutcome

Outcome = l.DoLogin(Login.Text, Password.Text)
Select Case Outcome
Case Security.LoginOutcome.lgBADLOGIN
Case Security.LoginOutcome.lgBADPASSWORD
Case Security.LoginOutcome.lgINACTIVE
Case Security.LoginOutcome.lgLOCKED
Case Security.LoginOutcome.lgLOGGEDIN
Session("User") = l
If l.IsAdmin Then
EnableFixtureEntry(True)
Else
EnableFixtureEntry(False)
End If
Label3.Text = "You are logged in as " & l.User
End Select

End Sub


When the page postsback to itself, I need that session variable saved, so that I can access it. Trouble is when I try to retrieve it, I get a null value.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

If IsPostBack Then
'Determine if the user is logged in.
Dim l As New Security()
l = Session("User")

If Not l Is Nothing Then
pnlPredictions.Controls.Add(New LiteralControl(l.User))
End If
End If

End Sub


Problem is that when it gets to evaluating whether l Is Nothing, it always returns true. Please can someone help me?Well, I always use
Session.Add("User",1)

1 being the value or whatever you assign to it.

Put a breakpoint on your assignment. After that line executes, type
? Session("User") in your Command Window. If you get a null or nothing, then it isn't setting in the first place.
Thanks for the response.

I have done that as well, and I get the same result.

When I do a response.write on the session("User") within the Button.Click Event, it does work.

The session variable seems to reset when the Postback to the page occurs.
Could you post the entire code of the page, aspx.vb along with the aspx ?

I also wonder if your class is not properly serializing. If it doesn't serialize properly it won't be stored in viewstate. However, ASP.NET compiler would most likely error when you attempt putting Session("user") = l.

This is what I would do, use Session.add("User","LOGGEDIN")
in your login code, then in the postback, put
dim st as string = Session.Item("User")

Put a breakpoint on the line right after you grab the Session.Item("user"), and see if st = "LOGGEDIN". If it does, we know its not some weird web configuration problem.

We would then ascertain your class does not properly serialize to a string (XML).
Strange things happen when trying to reteieve complex objects from viewstate or sessions whout a proper cast. A datagrid for example must be properly casted before it can be used...

try

dim s as new Security()

s = CType(Session("User"), Security)

0 comments:

Post a Comment