Solved

asp.net c#, session variable

Posted on 2014-11-17
2
610 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 34

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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