dcsimg
Results 1 to 5 of 5

Thread: Using 'Session'

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Using 'Session'

    Hello

    I am trying to come to terms with 'Session' in vb.net and have this in my userpage.aspx.vb file:

    Code:
     Protected Sub BtnLogout_Click(sender As Object, e As System.EventArgs) Handles BtnLogout.Click
    
            Session("Email") = String.Empty
            Session.RemoveAll()
            Session.Clear()
            Session.Abandon()
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1))
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.Cache.SetNoStore()
            Response.Redirect("index.aspx")
    
        End Sub
    The page that allows access to userpage.aspx is my log-in page. Do I also need to code something like

    Code:
    Session("Email") = String
    in my login.aspx.vb page?

    Thanks!

  2. #2
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Using 'Session'

    Where are you setting that session variable? I assume it is when they are at the login page? If so, then you would need to set that variable on that page in order to access it across other pages.

    You could have something like

    Session("Email") = txtEmail.text

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Using 'Session'

    Hello katman

    Thank you for your reply.

    When the user logs in, he is redirected to userpage.aspx where he can upload files. It is the only reason for a user to log in. In that same userpage.aspx, I have:

    Code:
       Protected Sub BtnLogout_Click(sender As Object, e As System.EventArgs) Handles BtnLogout.Click
    
            Session("Email") = String.Empty        
            Session.RemoveAll()
            Session.Clear()
            Session.Abandon()
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1))
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.Cache.SetNoStore()
            Response.Redirect("index.aspx")
    
        End Sub
    On the login.aspx.vb page itself, following your advice, I was going to have:

    Code:
    Session("Email") = Email.Text
    The related code on my logon.aspx file is this:

    Code:
    <asp:TextBox runat="server" ID="Email" TextMode="Email" />
    Does that look right or should all that aspx.vb code be placed in login.aspx.vb and not userpage.aspx.vb?

    Many thanks again.

    Regards

  4. #4
    New Member
    Join Date
    Aug 2019
    Posts
    6

    Re: Using 'Session'

    yes, this looks correct. Then on your userpage, you would access that variable like:

    lblEmail.text = Session("Email")

    This assumes you would add a label called "lblEmail". Putting in the code above would set the text for that label equal to your session value.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    469

    Re: Using 'Session'

    That's great katman, thanks you.

    I have given you some kudos but not sure it has registered.

    Regards

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width