Reputation: 7
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
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
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
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