1

I have a function that assigns values to a dictionary created in c#. After some computations in the function, the dictionary is compsed of a value and a key. I need to create a session save this dictionary in this session. Can anybody help?

5
  • You mean you want to create a dictionary of sessions? Please elaborate. Commented Sep 11, 2012 at 8:09
  • 1
    Session itself is a kind of dictionary, too, really - a series of values accessed by key. Are you looking to do something like this - stackoverflow.com/questions/4853403/… ? Commented Sep 11, 2012 at 8:09
  • You can store your dictionary in the Session Dictionary. I think you need to read some about asp.net cache, because store a Dictorionary in a Session, sounds like a chance of cache. Commented Sep 11, 2012 at 8:12
  • No the dictionary is compased of string and List<int>. I just need to add this dictianry to a session Commented Sep 11, 2012 at 8:12
  • @HanadyDaccache: What dash said is that you also could use the Session itself. For example: Session.Add("Item1",list1);Session.Add("Item2",list2); and get the items var list1 = (List<int>)Session["Item1"];. The only difference is that the session is not strongly typed. Commented Sep 11, 2012 at 8:21

1 Answer 1

3

Put simply, if you have a Dictionary<string, List<int>> then putting it in the session is not very complicated;

Dictionary<string, List<int>> myDict = new Dictionary<string, List<int>>();
HttpContext.Current.Session.Add("MyDictionary", myDict);

If you want to check if the Session already contains your dictionary, and only add it if it doesn't then:

if(!HttpContext.Current.Session.ContainsKey("MyDictionary"))
{
    HttpContext.Current.Session.Add("MyDictionary", myDict);
}

To get it out, remember that it's not typed, Session is effectively a dictionary of string, object so you'll have to cast:

if(HttpContext.Current.Session.ContainsKey("MyDictionary"))
{
    Dictionary<string, List<int>> myDict = HttpContext.Current.Session["MyDictionary"] as Dictionary<string, List<int>>;
}

However, you should also consider what the lifecycle of this object is going to be - Session is not stable in that it has a finite lifetime - it will disappear after a predetermined time of inactivity, for example, so if you want it to hang around for a longer time then you may need to consider alternative strategies.

There's a ton of documentation on Session over at MSDN and it's also a very common topic on Stack Overflow.

Remember also that, as Session is effectively a Dictionary<String, Object> managed by the .Net Framework and IIS, you could also do:

Session["MyValues"] = new List<Int32>();

And, of course, on retrieval:

List<Int32> myValues = Session["MyValues"] as List<Int32>();

Other alternatives include the Cache; if your object is going to be available to all users of the application then this might be a better solution. Please see this answer for more information.

I've also voted to close your question as I believe it's too localized, but I hope my answer helps you along the way.

Sign up to request clarification or add additional context in comments.

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.