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!

4
  • 1
    Why are you first doing a ContainsKey(key) and then a IsNullOrEmpty(key)? I would expect those tests to be reversed. Commented Feb 4, 2011 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 stackoverflow.com/questions/4854406/serialization-of-dictionary and support.microsoft.com/kb/311209 Commented Feb 4, 2011 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? Commented Feb 4, 2011 at 15:11
  • @el ronnoco - N - serialization only required if you need to persist / hydrate session. Commented Feb 5, 2011 at 14:50

1 Answer 1

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

4 Comments

Thanks Can you please clear it more "No need to "set the dictionary back".
Can we remove particular key from the Dictionary Session, something like SessionDictionary.Remove("FORMDATA", "panelOpen");
Of course: find the dictionary, and then use it's Remove(key) method.
thanks, Can you please give some example code using above code, how to remove the key

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.