user3005601
user3005601

Reputation: 7

use of session variable in C#

I am creating a function in C# in which I am declaring a session variable storing an integer value 45. Can I use this session variable(valued 45) in the same page but in different function?

Upvotes: 0

Views: 554

Answers (3)

Ajay
Ajay

Reputation: 2080

You can use session variable in entire application until that session variable destroyed .

After reading your question i understood that you need to know "State Management Techniques". see below links for "State Management"

http://www.codeproject.com/Articles/17191/ASP-Net-State-Management-Techniques

http://www.codeproject.com/Articles/31994/Beginners-Introduction-to-State-Management-Techniq

and for more you need to google.

Upvotes: 0

nZeus
nZeus

Reputation: 2504

Sure, e.g. if you created a new session-item using:

Session["MyVariable"] = 45;

you can get its value in the same way:

var value = Session["MyVariable"] as int?; 

UPDATED

Agree with julealgon, here is a better solution:

private const string _myVariableSessionKey = "MyVariable";
private int? MyVariable 
{
    get
    {
        return Session[myVariableSessionKey] as int?; 
    }
    set
    {
        Session[myVariableSessionKey] = value; 
    }
}

Upvotes: 3

Vaibhav Parmar
Vaibhav Parmar

Reputation: 643

yes you can use it.Session variable is used throughout the application until particular user log out.or session time out event occurs.

Upvotes: 2

Related Questions