Solved

asp.net c#, session variable

Posted on 2014-11-17
2
670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 36

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

Stressed Out?

Watch some penguins on the livecam!

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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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