0

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?

3 Answers 3

3

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; 
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's probably a good idea to wrap this get/set behavior into a property so that the hardcoded strings are kept to a minimum. I'd say it's even better to create a private const string to store the key name and then use it inside the property accessors.
2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.