Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys. I have some sessions in ASP.net as follow . In file Test.aspx.cs have some session

C#
HttpContext.Current.Session["Id"] = obj.USER_MOD_ID;
HttpContext.Current.Session["Group"] = obj.GROUP_MOD_ID;
HttpContext.Current.Session["Region"] = obj.REGION_ID;


And then i create some class , I want to get value of session.

First , i create a class MySession.cs

C#
public class MySession
   {
       // private constructor
       private MySession()
       {
           value_group = "Group";
           //value_id = "Id";
           //value_region = "Region";
       }
       // Gets the current session.
       public static MySession Current
       {
           get
           {
               MySession session =(MySession)HttpContext.Current.Session["Group"];
               if (session == null)
               {
                   session = new MySession();
                   HttpContext.Current.Session["Group"] = session;
               }
               return session;
           }
       }
       // **** add your session properties here, e.g like this:
       public string value_group { get; set; }
       //public string value_id { get; set; }
       //public string value_region { get; set; }
   }


Second, i create class Test.cs and i want get value of session

C#
string value_group = MySession.Current.value_group.ToString();
               //string value_id = MySession.Current.value_id.ToString();
               //string value_region = MySession.Current.value_region.ToString();


But it's get error

C#
Object reference not set to an instance of an object 


Thank guys.
Posted

C#
MySession session = (MySession)HttpContext.Current.Session["Group"];

This casting is the issue.
Try using this instead:
C#
MySession session = HttpContext.Current.Session["Group"] as MySession;
 
Share this answer
 
Comments
headshot9x 4-Jun-15 0:07am    
I don't understand the other session where values , Id and Region ? and I try but it's not work.
if the Current is returning session instant, change the code as below
C#
public class MySession
   {
       // private constructor
       private MySession()
       {
           Group= "Initial Group Value";
       }
       // Gets the current session.
       public static MySession Current
       {
           get
           {
               MySession session =(MySession)HttpContext.Current.Session["MySession"];
               if (session == null)
               {
                   session = new MySession();
                   HttpContext.Current.Session["MySession"] = session;
               }
               return session;
           }
       }

       public string Group { get; set; }

   }


then you need to access session value as

C#
string value_group = MySession.Current.Group;
 
Share this answer
 
Comments
headshot9x 4-Jun-15 0:10am    
Hi guys, not the attributes of the session which is the value of the session
HttpContext.Current.Session["Id"] = obj.USER_MOD_ID;
HttpContext.Current.Session["Group"] = obj.GROUP_MOD_ID;
HttpContext.Current.Session["Region"] = obj.REGION_ID;
I try but it's not work.

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