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?

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

This code fails in both the production and development server.
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.