How to Read/Write the Session variables in ASP.NET

The following example shows how to create session variables in an ASP.NET page for the first and last name of a user, and set them to values retrieved from TextBox controls.
  1. Sessions are used to store the data for the user just like cookies.
  2. The Session object stores information about, or change settings for a user session.
  3. Variables are stored in a Session object hold information about one single user.And are available to all pages in one application.
  4. Common information stored in session variables are name, id, and preferences.
  5. The server creates a new Session object for each new user, and destroys the Session object when the session expires.
Set values to session from Textbox controls:
  1. Session["UserName"] = UserNameTextBox.Text;  
  2. Session["Password"] = PasswordTextBox.Text;  
On ASPX Page:
  1. string loggedUsername = Session["UserName"as string;  
  2. mylabel.Text = "Currently Logged User is" + (loggedUsername != null) ? loggedUsername : "Unknown Person";  
Thanks for learning my blogs !!!