Solved

asp.net c#, session variable

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 121
AutoHotkey ActiveX Component Error 8 56
Setting runtime form location 4 19
Getfiles in vb.net 28 24
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now