skip to Main Content

I have two web forms in ASP.net. In the first page is set a session variable like so:

HttpContext.Current.Session("myTestVariable") = "ABCD"

On the second page I try to read the session variable like so:

dim myString as string = HttpContext.Current.Session("myTestVariable")

The string is null on the second page.

In web.config I have the following entry:

sessionState mode="InProc"  timeout="60"

What am I missing?

2

Answers


  1. Chosen as BEST ANSWER

    I could not get this working. Maybe something to do with my unique development environment. Based on my experience, I have now implemented a REDIS database for all my session variable needs. This seems to be working satisfactory.

    As an old programmer, I find it hard when things are not under my direct control. I trust that ASP.NET will fail from time-to-time. I can't take the risk that my hours of testing in a Development environment does not translate to the production environment. Managed hosts are the way to go. But you have no control over the integrity of the production environment.


  2. What you have looks correct.

    So, say in PageA.aspx, I have this markup:

            <h3>Enter value to pass to page B</h3>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
    
            <asp:Button ID="cmdJump" runat="server" Text="Jump to page B"
                OnClick="cmdJump_Click"
                />
    

    And code behind for the button is:

    Protected Sub cmdJump_Click(sender As Object, e As EventArgs)
    
        HttpContext.Current.Session("myTestVariable") = TextBox1.Text
        Response.Redirect("PageB.aspx")
    
    End Sub
    

    And on PageB.aspx, this markup:

            <h3>Session Value passed from Page A is</h3>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    

    And code behind for PageB.aspx is:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        Dim myString As String = HttpContext.Current.Session("myTestVariable")
    
        Label1.Text = myString
    
    End Sub
    

    Hence, the result is this:

    enter image description here

    So, there must be some additional information you are leaving out, as we are unable to reproduce your error.

    What you have looks just fine.

    Does the above sample code not work say on your developer computer, but fails on the production server?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search