Syspect
Syspect

Reputation: 921

Not able to use Sessions in ASP MVC3

I start the page from a landing page where I call a function from a Controller, to assign values to some sessions. But I seem to not be able to use them, as I get Object reference not set to an instance of an object. exception on the first line of the body of the following function:

public void assignNewUserToSession(string currentButtonID, string user, string app)
        {
            Session["buttonID"] = currentButtonID;
            Session["userID"] = user;
            Session["appID"] = app;
        }

This exception is usually thrown when one is using a variable that has not been initialized, that is = to null or nothing. But as far as I know, Sessions don't have to be initialized in any other way than I have done it in the code above.

Here is where I call the upper function:

public void landingPage()
        {
            FC.assignNewUserToSession("34", "asd", "gsds");

            Media(); // This is the view Controller for another page.
        }

What might be the problem ?

Upvotes: 1

Views: 305

Answers (2)

Amit
Amit

Reputation: 15387

Try this

   HttpContext.Current.Session["buttonID"]=currentButtonID;
   HttpContext.Current.Session["userID"] = user;
   HttpContext.Current.Session["appID"] = app;

Upvotes: 1

Manoj
Manoj

Reputation: 883

Try

HttpContext.Current.Session 

instead of

Session 

Hope this will help you.

Thanks

Upvotes: 2

Related Questions