Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
not able to create session and retrieve values from session

What I have tried:

Session["USERID"] = "manish";


showing error: "
Object reference not set to an instance of an object.
" is

same error is also coming while creating cookie also
Posted
Updated 22-Jan-19 6:19am

You haven't said where you are writing this code. Using Session on its own tends to only work in a controller. Elsewhere you might need to use

System.Web.HttpContext.Current.Session["USERID"] = "xyz";


Note that it is designed that way for a reason, if that is the issue then you might be trying to access the Session somewhere you shouldn't.
 
Share this answer
 
If your project is running on ASP.NET Core, you will need to add in a few items to the ConfigureServices portion of your Startup.cs file
C#
public void ConfigureServices(IServiceCollection services) {
  // ...  your other items 
  services.AddDistributedMemoryCache();
  services.AddSession();
}
More Info: Session and app state in ASP.NET Core | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900