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 { } }
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-"); }}
This is the code to initialize settings (seems it is working ok):
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.Sessio
Why I couldn't use just: Session.Add("MySettings")=
To restore:
settings = HttpContext.Current.Sessio
or: settings = Session["MySettings"];