Results 1 to 4 of 4

Thread: Sessions variables getting lost

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    203

    Sessions variables getting lost

    Hey,
    I've spend ages looking into the problem, but I cant find it.

    This is my page code

    Code:
     Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
                Dim blnLogin As Boolean
            blnLogin = Login(txtUserLogin.Text, txtPassword.Text)
    
                If blnLogin = True Then
                Session("LoggedIn") = ""
                Session("LoggedInUser") = txtUserLogin.Text
                Session("LoggedInID") = GetUserID(txtUserLogin.Text)
                    lblNotice.Text = "Login succeeded: " '& GetFriends(Session("LoggedInUser"))
                Else
                    lblNotice.Text = "User not found"
            End If
        End Sub
    
        Protected Sub btnAddToWall_Click(sender As Object, e As EventArgs) Handles btnAddToWall.Click
            MsgBox(Session("LoggedInID"))
        End Sub
    So after the user presses the Login button there should be 3 variables stored in a session, this works (checked using trace='true')

    Session State
    Session Key Type Value
    LoggedIn System.String
    LoggedInUser System.String User
    LoggedInID System.Int32 2

    That's fine. Now I click on the AddToWall button to check if I can get the LoggedInID, it shows an empty messagebox.

    In the trace there are no Sessions keys left.
    How can I make it keep the keys after postback/after clicking on buttons?

    Note: When calling GetFriends(Session("LoggedInUser")) in the same sub as the Login button the variable does get passed to the class with sub GetFriends()

    Here's my web.config
    Code:
    <?xml version="1.0"?>
    <configuration>
    
      <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <sessionState timeout="30" />
      </system.web>
    
    </configuration>
    Last edited by vixez; Jan 28th, 2012 at 09:46 AM.

  2. #2
    Member
    Join Date
    Jan 2012
    Posts
    46

    Re: Sessions variables getting lost

    Have you tried getting any other Session data to AddToWall event? are they all blank if you have? your code looks like its all right, but I am pretty new to VB, but it looks like it should be working. The keys should be staying automatically. And it looks like like that data is staying cause you have traced it...Have you tried saving the data in session as something else then passing that in the MsgBoX?

    Protected Sub btnAddToWall_Click(sender As Object, e As EventArgs) Handles btnAddToWall.Click

    Dim test As Int32 = Session("LoggedInID")
    MsgBox(test)

    End Sub

    just to see if it passes between the subs, worth a try, but other then that, i don't see anything wrong with your code
    Last edited by jdogg; Jan 31st, 2012 at 11:45 AM. Reason: Mistyped

  3. #3
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Sessions variables getting lost

    Are you clearing the session variable in Page_Load event?
    __________________
    Rate the posts that helped you

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Sessions variables getting lost

    Hello,

    You may want to guard against things like this by checking the IsNewSession property:

    http://msdn.microsoft.com/en-us/libr...ewsession.aspx

    If for some reason the Session is getting recreated, you should be able to detect this, and redirect the user to the log in page.

    Gary

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