0

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?

3 Answers 3

0

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?

1
  • This code fails in both the production and development server. Commented Aug 7 at 7:02
0

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.

0

A related answer. Suddenly my sessionId is not performing consistently across web pages. I get a new sessionId every time I refresh a page. Now changed the session provider from INPROC to REDIS. The problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.