CodeBeginner
CodeBeginner

Reputation: 67

ASP.NET how to use Sessions, when and where to declare it?

I am trying to retrieve some data from db and store it some Session variable in order to have it in _Layout.cshtml on all pages, no matter what page the user will access from the start. Global.asax:

protected void Application_Start() { ... Manager mng = new Manager(); HttpContext.Current.Session["company-cellphone"] = mng.GetContacts().CompanyCellphone; }

Error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Upvotes: 1

Views: 2462

Answers (3)

Scott Hannen
Scott Hannen

Reputation: 29222

I'm going to go out on a limb and guess that this is a single global value to be viewed by all users. In that case you could store the value in HttpApplicationState rather than HttpSessionState:

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        Application["YourValue"] = "SomeValue";
    }
}

I'm not necessarily advocating its use. But just as Session can store user-specific values, Application stores values that are global to the application.

Upvotes: 0

Clint B
Clint B

Reputation: 4700

Application_Start runs before any sessions can be created. And a session is specific to a single client connecting to your application.

You can create a static class and store the company-cellphone value in it.

In your Models folder create a new class file named Global.cs in that file create a static class with properties that will hold your application level information.

public static class Global
{
    static string companyCellPhone;

    public static string companyCellPhone
    {
    get
    {
        return this.companyCellPhone;
    }
    set
    {
        this.companyCellPhone= value;
    }
}

Then your Application_Start method would look something like this:

protected void Application_Start()
{
    ...
    Manager mng = new Manager();
    Global.companyCellPhone = mng.GetContacts().CompanyCellphone;
}

Upvotes: 0

esiprogrammer
esiprogrammer

Reputation: 1438

you are trying to access the session from Application_Start but there is no live Session yet.

session is not available in all events of global.asax

as a workaround try this:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
     if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)
     {
            HttpContext context = HttpContext.Current;
            ...
            Manager mng = new Manager();
            HttpContext.Current.Session["company-cellphone"] = mng.GetContacts().CompanyCellphone;
     }
 }

I'm not sure about your requirement but I would recommend to access the session in controller.initialize method

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
   base.Initialize(requestContext);
   //// access session here
   requestContext.HttpContext.Session["company-cellphone"]=mng.GetContacts().CompanyCellphone;
}

Upvotes: 1

Related Questions