views:

50

answers:

3

hi,

i have an application wherein i have incorporate a "Remember Me" feature for the login screen. I do this by creating a cookie when the user logs in for the first time, so next time when the user visits the site i get the cookie and load the user information.

i have written the code for loading user information in a common class in the App_Code folder...and all my pages inherit from this class.

code for loading the user info is as follows:

public static void LoadUserDetails(string emailId)
{
    UsersEnt currentUser = UsersBL.LoadUserInfo(emailId);

    if (currentUser != null)
        HttpContext.Current.Session["CurrentUser"] = currentUser;
}

Now the problem is i get an "Object reference" error when i try to store the currentUser object in the session variable (even though the currentUser object is not null). However the password property in the currentUser object is null.

Am i getting the error because of this...or is there some other reason??

thank you

+6  A: 

Assuming it's the final line which is causing the problem, that suggests that either HttpContext.Current or HttpContext.Current.Session is null. I suggest you find out which it is, and then work out why.

Jon Skeet
+2  A: 

HttpContext.Current.Session is probably null.

Code that uses the State has to be placed after the AcquireRequestState Event has called. See the page lifecycle for more information.

Try putting your code after or inside the Page_Load method.

SLC
A: 

If it is throwing the exception on the line:

HttpContext.Current.Session["CurrentUser"] = currentUser;

Then the only other explanation is that HttpContext.Current.Session is null.

Ben Robinson