1

I have got below code to GET dictionary type of session variable value. Please see the below code

In my code, I just use below code to get any value from my session variable:

string panelOpen = SessionDictionary.GetValue("FORMDATA", "panelOpen");

public class SessionDictionary
{
    public static string GetValue(string dictionaryName, string key)
    {
        string value = string.Empty;
        try
        {
            if (HttpContext.Current.Session[dictionaryName] != null)
            {
                Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
                if (form.ContainsKey(key))
                {
                    if (!string.IsNullOrEmpty(key))
                    {
                        value = form[key];
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
        }
        return value;
    }
}

Now I want to write a method to SET the value for particular session key, for example

SessionDictionary.SetValue("FORMDATA", "panelOpen") = "First";

Now if I again go for below code it should give me "First" for my panelOpen key.

string panelOpen = SessionDictionary.GetValue("FORMDATA", "panelOpen");

Please suggest!

curtisk
  • 19,950
  • 4
  • 55
  • 71
Manoj Singh
  • 7,569
  • 34
  • 119
  • 198
  • 1
    Why are you first doing a ContainsKey(key) and *then* a IsNullOrEmpty(key)? I would expect those tests to be reversed. – Hans Kesting Feb 04 '11 at 11:49
  • 1
    Be careful when using Dictionary in SessionState as it isn't serializable. This might have scalability issues if you use SQL Persistence on your sessions http://stackoverflow.com/questions/4854406/serialization-of-dictionary and http://support.microsoft.com/kb/311209 – StuartLC Feb 04 '11 at 11:56
  • @nonnb Does Session in ASP demand that an object implements `IXMLSerialiable`? If so, how can it be possible to store a Dictionary in Session at all? – El Ronnoco Feb 04 '11 at 15:11
  • @el ronnoco - N - serialization only required if you need to persist / hydrate session. – StuartLC Feb 05 '11 at 14:50

1 Answers1

2

The "SetValue" would be almost identical, except for the line value = form[key];. That should become form[key] = value;.

No need to "set the dictionary back into the session" as the reference to that same dictionary is still present in the session.

Examples:

Setting a value

public static void SetValue(string dictionaryName, string key, string value)
{
  if (!String.IsNullOrEmpty(key))
  {
    try
    {
        if (HttpContext.Current.Session[dictionaryName] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
            if (form.ContainsKey(key))
            {
                form[key] = value;
            }
        }
    }
    catch (Exception ex)
    {
        Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
    }
  }
}

Removing a value:

public static void RemoveValue(string dictionaryName, string key)
{
  if (!String.IsNullOrEmpty(key))
  {
    try
    {
        if (HttpContext.Current.Session[dictionaryName] != null)
        {
            Dictionary<string, string> form = (Dictionary<string, string>)HttpContext.Current.Session[dictionaryName];
            form.Remove(key); // no error if key didn't exist
        }
    }
    catch (Exception ex)
    {
        Logger.Error("{0}: Error while checking Session value from Dictionary", ex, "SessionDictionary");
    }
  }
}
Hans Kesting
  • 38,117
  • 9
  • 79
  • 111