Click here to Skip to main content
15,886,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to access Session value from class.

Example:

C#
public class a
{
    int a=10;
}
public static class b
{
    int b=10;
}


Means how can I access these value class a and b value
Please let me explain about Session in depth?

Thanks in advance
Posted
Updated 23-Mar-12 5:46am
v6
Comments
[no name] 23-Mar-12 7:23am    
<Pre> tag added.
Ajith The Great 23-Mar-12 7:25am    
By the way you cannot use Session Variable in Class..

You may use Static Properties

C#
public class TestClass
{
    public static int  PropertyA {

        set{ HttpContext.Current.Session["objA"] = value;}

        get{
                return(HttpContext.Current.Session["objA"]==null
                ?0:int.parse(HttpContext.Current.Session["objA"].ToString())
                       );
            }
                         }
}


You Can get and set easily in session using this Property.

as In
C#
TestClass.PropertyA=10;


It will save value in Session which can be retrieved using same property

i-e

C#
Response.Write(TestClass.PropertyA.ToString());
 
Share this answer
 
Comments
priya from Madras 23-Mar-12 7:48am    
Thanks Hammad if i need to get a value of class b mean using the same way ?

if

public static class b
{
}
mean after the class a
Hammad Abbasi 23-Mar-12 8:56am    
you may create as many properties or classes as you want , The key is to use static properties.

public class b
{
public static int b{
set{ HttpContext.Current.Session["objA"] = value;}

get{
return(HttpContext.Current.Session["objB"]==null
?0:int.parse(HttpContext.Current.Session["objB"].ToString())
);
}
}
}
priya from Madras 23-Mar-12 7:51am    
How can i access the class b and a in at the same time
Hi,


//For Boolean Variable.

C#
Session("Flag") = true; //Assign Value For Session Variable

if ((bool)(Session("Flag")))
{
    //do something...
}



In The Same Way You can use For String as well as objects Like Datatable,Dataset,etc.

Hope This May help You...
 
Share this answer
 
Comments
priya from Madras 23-Mar-12 7:26am    
ya but in the example given by me , can u explain in that format
follow the Hammad Abbasi answer.this is right way to access the session value from one class to another
 
Share this answer
 
I always use a wrapper class around the ASP.NET session to simplify access to
C#
public class MySession
{
    // private constructor
    private MySession()
    {
      Property1 = "default 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;
      }
    }

    // **** add your session properties here, e.g like this:
    public string Property1 { get; set; }
    public DateTime MyDate { get; set; }
    public int LoginId { get; set; }
}
 
Share this answer
 
v2
Comments
André Kraak 23-Mar-12 8:44am    
Edited solution:
Added pre tags
fjdiewornncalwe 4-Apr-13 15:39pm    
Plagiarized: source

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