Avatar of Mike Eghtebas
Mike Eghtebas
Flag for United States of America asked on

asp.net c#, session variable

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.
C#.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
Mike Eghtebas

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Miguel Oz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Mike Eghtebas

ASKER
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"];
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23