0

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

3 Answers 3

7

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.

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

Comments

2

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.

Comments

0

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.

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.