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?
-
You mean you want to create a dictionary of sessions? Please elaborate. – Nikhil Agrawal Sep 11 '12 at 08:09
-
1Session itself is a kind of dictionary, too, really - a series of values accessed by key. Are you looking to do something like this - http://stackoverflow.com/questions/4853403/accessing-dictionary-in-session ? – dash Sep 11 '12 at 08: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. – Alberto León Sep 11 '12 at 08:12
-
No the dictionary is compased of string and List
. I just need to add this dictianry to a session – Hanady Sep 11 '12 at 08: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
)Session["Item1"];`. The only difference is that the session is not strongly typed. – Tim Schmelter Sep 11 '12 at 08:21
1 Answers
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.