1

I'm using ASP.NET for a project. I'm trying to set a session variable and access its value in the aspx file.

In VB.NET, I set the session variable like this:

Partial Class MainSite_Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("TestVariable") = "Lorem ipsum"
    End Sub
End Class

And in the corresponding aspx file, I can access its value using <%=Session("TestVariable")%>.

This works fine. But, now let's say I rewrite this in C#, and try to set the session variable similarly:

public partial class Mainsite_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
        Session["TestVariable"] = "Lorem ipsum";
    }
}

And once again, in the aspx file, I try to access its value using <%=Session["TestVariable"]%>.

In this case, Session["TestVariable"] is null. The value doesn't seem to get assigned. I've also tried changing Session to HttpContext.Current.Session with no luck.

Am I setting the session variable wrong in C#?

Interestingly, if I set the session variable in the MasterPageFile, I can then access it in the aspx file. But not if I place it in the Page_Load method of my Mainsite_Default class. However, if I set the variable in the Page_Load method in VB.NET, it works fine.

mason
  • 31,774
  • 10
  • 77
  • 121
JoshG
  • 6,472
  • 2
  • 38
  • 61

3 Answers3

0

It turns out that, AutoEventWireup was set to false in the page directive at the top of the page. This was preventing the page load event from firing. When I changed it to true, everything began working as expected.

After further investigation, it looks like it worked in VB.NET because the Handles Me.Load line explicitly specified which method to call when the page loaded, even though AutoEventWireup was set to false. But C# does not have a Handles keyword, so the event has to assigned either using the += operator, or via AutoEventWireup.

Thanks to everyone for your suggestions. Collectively, they definitely guided me toward the solution.

JoshG
  • 6,472
  • 2
  • 38
  • 61
-1

Try this to declare session

public static string TestSessionValue 
{ 
    get 
    {
        object value = HttpContext.Current.Session["TestSessionValue"];
        return value == null ? "" : (string)value;
    }
    set 
    {
        HttpContext.Current.Session["TestSessionValue"] = value;
    }
}

already solve : references : How to declare session variable in C#?

Lemons
  • 107
  • 1
  • 13
  • This code checks if it is null; however, it should not be null. The question is why it is null when it is clearly being set. – CodingYoshi Feb 06 '18 at 03:56
  • it's is default condition to handle error in return statement if there's no session stored when u call data from session – Lemons Feb 06 '18 at 04:11
  • But it is stored so why would it be null? – CodingYoshi Feb 06 '18 at 04:13
  • if session null return " ", if not return value – Lemons Feb 06 '18 at 04:13
  • 1
    @FadlyRahman It's stated in the question that the variable gets set. Or at least that there's code that's present that should set it. The code in your answer does nothing different except cast an object to a string, which wouldn't change anything. – mason Feb 06 '18 at 04:32
  • object value = HttpContext.Current.Session["TestSessionValue"]; the value object would be null if there's no session – Lemons Feb 06 '18 at 04:32
  • 1
    @FadlyRahman You mean it would be null if there's no `TestSessionValue` in the Session? Sure. But that doesn't help anything. – mason Feb 06 '18 at 04:35
-1

Session state can be null in some cases in order to be sure you are not getting a NullReference exception, it is always better to check it first. It is also prefered to store the string in a variable then assign it to a session variable.

public partial class Mainsite_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {
        string str = "Lorem ipsum";
        Session["TestVariable"] = str;
    }
}

And while accessing its value

if (Session["TestVariable"] != null)
{
    string value = Session["TestVariable"].ToString(); // Don't forget ToString()
}

Refer to this answer for details on when a session variable can be null. Here is a part of the answer

Yes, the Session object might be null, but only in certain circumstances, which you will only rarely run into:

  1. If you have disabled the SessionState http module, disabling sessions altogether
  2. If your code runs before the HttpApplication.AcquireRequestState

  3. Your code runs in an IHttpHandler, that does not specify either the IRequiresSessionState or IReadOnlySessionState interface. If you only have code in pages, you won't run into this. Most of my ASP .NET code uses Session without checking for null repeatedly. It is, however, something to think about if you are developing an IHttpModule or otherwise is down in the grittier details of ASP .NET.

Elham Kohestani
  • 3,013
  • 3
  • 20
  • 29