Results 1 to 5 of 5

Thread: Session Variables aren't retrieved during Postback

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    London
    Posts
    44

    Session Variables aren't 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"

    VB Code:
    1. Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
    2.         'Login into the system
    3.         Dim l As New Security()
    4.         Dim Outcome As PPPL.Security.LoginOutcome
    5.  
    6.         Outcome = l.DoLogin(Login.Text, Password.Text)
    7.         Select Case Outcome
    8.             Case Security.LoginOutcome.lgBADLOGIN
    9.             Case Security.LoginOutcome.lgBADPASSWORD
    10.             Case Security.LoginOutcome.lgINACTIVE
    11.             Case Security.LoginOutcome.lgLOCKED
    12.             Case Security.LoginOutcome.lgLOGGEDIN
    13.                 Session("User") = l
    14.                 If l.IsAdmin Then
    15.                     EnableFixtureEntry(True)
    16.                 Else
    17.                     EnableFixtureEntry(False)
    18.                 End If
    19.                 Label3.Text = "You are logged in as " & l.User
    20.         End Select
    21.  
    22.     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.

    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.  
    4.         If IsPostBack Then
    5.             'Determine if the user is logged in.
    6.             Dim l As New Security()
    7.             l = Session("User")
    8.  
    9.             If Not l Is Nothing Then
    10.                 pnlPredictions.Controls.Add(New LiteralControl(l.User))
    11.             End If
    12.         End If
    13.  
    14.     End Sub

    Problem is that when it gets to evaluating whether l Is Nothing, it always returns true. Please can someone help me?

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Well, I always use
    VB Code:
    1. 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
    Code:
     ? Session("User")
    in your Command Window. If you get a null or nothing, then it isn't setting in the first place.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    London
    Posts
    44

    I have done that too.

    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.

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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
    VB Code:
    1. Session.add("User","LOGGEDIN")
    in your login code, then in the postback, put
    VB Code:
    1. 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).
    Last edited by nemaroller; Mar 7th, 2004 at 07:53 PM.

  5. #5
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    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
    VB Code:
    1. dim s as new Security()
    2.  
    3. s = CType(Session("User"), Security)

Posting Permissions

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



Click Here to Expand Forum to Full Width