Click here to Skip to main content
15,917,964 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
view state.cs file

XML
protected void Button1_Click(object sender, EventArgs e)
      {
          Dictionary<string, string> dictionary = new Dictionary<string, string>();
          dictionary.Add("txtUserName", txtUserName.Value);
          dictionary.Add("txtPassword", txtPassword.Value);
          ViewState["User"] = dictionary;
          Session["Dictionary"] = dictionary;
          Response.Redirect("Session.aspx");
      }

session.cs file

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            var user = ViewState["User"];

        }



I have entered 2 values in viewstate.cs page and stored the values in session and viewstate and redirected to session.cs page.
now,i want to read the values from the session in session.cs page.
How can i do it?
Posted

The following is the way how you can retrieve values from a session variable.

Dictionary<string,> variableName = (Dictionary<string,>)Session["YourSessionVariableName"];


Note: Do not perform after Session.Abandon()/Remove() etc. Otherwise you can expect an Exception)

EDIT MB : Changed to cast session as dictionary object.
 
Share this answer
 
v3
Comments
AnirudhKalva 14-Jul-11 2:30am    
I tried your solution.
string variableName = Session["Dictionary"].ToString();
Label1.Text= variableName;
System.Collections.Generic.Dictionary`2[System.String,System.String] this is what i get when i passed the string variable to the label.
if(Session["Dictionary"] != null)
{
Dictionary<string,string> Dictionary1 = ((Dictionary<string,string>)Session["Dictionary"];
}
else
{
//
}
 
Share this answer
 
Code similar to dictionary = Session["Dictionary"]; should work.
 
Share this answer
 
v2
string samplename=Session["somename"].tostring();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900