Mike Eghtebas
asked on
asp.net c#, session variable
The following code at line 3 produces an error.
if (HttpContext.Current.Sessi on.ContainsKey("MySettings"))
Question: How can I correct my code?
Error: 'System.Web.SessionState.H ttpSession State' does not contain a definition for 'ContainsKey' and no extension method 'ContainsKey' accepting a first argument of type 'System.Web.SessionState.H ttpSession State' could be found (are you missing a using directive or an assembly reference?)
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.
if (HttpContext.Current.Sessi
Question: How can I correct my code?
Error: 'System.Web.SessionState.H
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
{
}
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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"];