Solved

asp.net c#, session variable

Posted on 2014-11-17
2
601 Views
Last Modified: 2014-11-17
The following code at line 3 produces an error.

if (HttpContext.Current.Session.ContainsKey("MySettings"))

Question: How can I correct my code?

Error: 'System.Web.SessionState.HttpSessionState' does not contain a definition for 'ContainsKey' and no extension method 'ContainsKey' accepting a first argument of type 'System.Web.SessionState.HttpSessionState' could be found (are you missing a using directive or an assembly reference?)  
Dictionary<string, string> settings = new Dictionary<string, string>();

 protected void Page_Load(object sender, EventArgs e)
{
            if (HttpContext.Current.Session.ContainsKey("MySettings"))
        {
            //HttpContext.Current.Session.Add("MySettings", settings);  // this line used elsewhere

            // store back settngs from last session
            settings = HttpContext.Current.Session["MySettings"] as Dictionary<string, string>;
        }
        else
        {
            upsdateSettings(listBoxes, "initialize"); // settings object gets initialized
        }

        if (IsPostBack)

        {
 
        }
        else
        {

        }
    }

Open in new window


FYI, I have located part of this code on the web. The declaration was like:

Dictionary<string, List<int>> settings = new Dictionary<string, List<int>>();

Could be this why I am getting this error? I have changed List<int> to string.
0
Comment
Question by:Mike Eghtebas
2 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40449040
You need to check the key existence by comparing to null.
In code above replace:
if (HttpContext.Current.Session.ContainsKey("MySettings"))
with:
if (HttpContext.Current.Session["MySettings"] != null)
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 40449066
Hi Miguel,

This is the code to initialize settings (seems it is working ok):
  
private void upsdateSettings(Dictionary<string, bool> listBoxes, string intilaizeOrSessionVariables)
 {
         //FYI,  intilaizeOrSessionVariables is not used, will be deleted.

        settings.Clear();   //??? is this necessary

        foreach (string box in (from listBox in listBoxes where listBox.Value select listBox.Key))
        {
            ListBox lb = new ListBox() { ID = "lst" + box };
            settings.Add(box.ToString(), "-All-");
        }
}

Open in new window


Question: Could you please check my syntax to store settings in the session variable and restore it from the session variable below:

To store:
HttpContext.Current.Session.Add("MySettings", settings);

Why I couldn't use just: Session.Add("MySettings")=settings;

To restore:
settings = HttpContext.Current.Session["MySettings"] as Dictionary<string, string>

or: settings = Session["MySettings"];
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question