ASP.NET Store, Retrieve Value In Session Using C#, VB.NET

What is a Session State in ASP.NET?

 
Session State is utilized to store the values of a client's browser session. A browser session is the duration of a user's website visit. Once the browser is closed, the session is finished. If the same user comes back to the website, a new session will be created. Session enables the data to be put away in one page and accessed on another page and supports any kind of item. In numerous sites, we can see its usefulness. For example - Once we are logged into a site, the site demonstrates our username (logged in user info) on every page. This happens because the site stores the username in a session.
 
When a client visits a web page, a new session is created with a unique session ID. The session ID is deleted once the user closes the current browser instance. 
 
Let's test this.
 
Create an ASP.NET website using Visual Studio.
 
Open Default.aspx page and replace the current page content with the following code.
 
Defaulf.aspx 
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head id="Head1" runat="server">  
  3.    <title>Asp.net Session State Example in C#, VB.NET</title>  
  4. </head>  
  5. <body>  
  6.    <form id="form1" runat="server">  
  7.    <div>  
  8.       <h3>SessionStateData.aspx</h3>  
  9.    <table>  
  10.       <tr>  
  11.          <td>FirstName:</td><td><asp:TextBox ID="txtfName" runat="server"/></td>  
  12.        </tr>  
  13.       <tr>  
  14.          <td>LastName:</td><td><asp:TextBox ID="txtlName" runat="server"/></td>  
  15.       </tr>  
  16.          <tr><td></td>      <td> <asp:Button ID="btnSubmit" runat="server" Text="Set SessionState Data"OnClick="btnSubmit_Click" /></td></tr>  
  17.    </table>  
  18.    </div>  
  19. </form>  
  20. </body>  
  21. </html>  
Now, add following namespaces in your code-behind.
 
C# Code
  1. using System;  
  2. After that write the following code in button click  
  3. protected void Page_Load(object sender, EventArgs e) {}  
  4. // Set Session values during button click  
  5. protected void btnSubmit_Click(object sender, EventArgs e) {  
  6.     Session["FirstName"] = txtfName.Text;  
  7.     Session["LastName"] = txtlName.Text;  
  8.     Response.Redirect("Default2.aspx");  
VB.NET Code
  1. Partial Class SessionStateExample  
  2. Inherits System.Web.UI.Page  
  3. Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs)  
  4. End Sub  
  5. ' Set Session values during button click  
  6. Protected Sub btnSubmit_Click(ByVal sender As ObjectByVal e As EventArgs)  
  7. Session("FirstName") = txtfName.Text  
  8. Session("LastName") = txtlName.Text  
  9. Response.Redirect("Default2.aspx")  
  10. End Sub  
  11. End Class 

In the above code, what we're doing is storing first name and last name in two Session variables. Once this data is stored, it will be available to the application during that session on every page. 
 
Now, right-click on your website in Visual Studio Solution Explorer > select Add new item > select Web Form > give name as Default2.aspx.
 
Open the Default2.aspx page and write the following code.
 
Default2.aspx
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head runat="server">  
  3.    <title>Untitled Page</title>  
  4. </head>  
  5. <body>  
  6.    <form id="form1" runat="server">  
  7.       <div>  
  8.          <h3>Default2.aspx</h3>  
  9.             <table>  
  10.                <tr>  
  11.                   <td colspan="2">Welcome <b><asp:Label ID="lblString" runat="server"/></b></td>  
  12.                </tr>  
  13.                <tr>  
  14.                   <td>Your FirstName: </td><td><b><asp:Label ID="lblfName" runat="server"/></b></td>  
  15.                </tr>  
  16.                <tr>  
  17.                   <td>Your LastName </td><td><b><asp:Label ID="lbllName" runat="server"/></b></td>  
  18.                </tr>  
  19.                <tr><td></td><td> </td></tr>  
  20.             </table>  
  21.          </div>  
  22.       </form>  
  23.    </body>  
  24. </html>  
Now, add the following namespaces in your code-behind.
 
C# Code
  1. using System;  
After that, write the following code on the button click event handler. As you can see from this code, we're reading back the session variables we stored data in the previous page. We're also displaying the first name and the last name read from the session variables in a label.
  1. protected void Page_Load(object sender, EventArgs e) {  
  2.     if (!IsPostBack) {  
  3.         if (Session["FirstName"] == null && Session["LastName"] == null) {  
  4.             Session["FirstName"] = "Sarthak";  
  5.             Session["LastName"] = " Varshney";  
  6.             lblString.Text = "Welcome " + Session["FirstName"] + Session["LastName"];  
  7.         } else {  
  8.             lblString.Text = Session["FirstName"] + " " + Session["LastName"];  
  9.             lblfName.Text = Session["FirstName"].ToString();  
  10.             lbllName.Text = Session["LastName"].ToString();  
  11.         }  
  12.     }  

VB.NET Code

  1. Partial Class Default2  
  2. Inherits System.Web.UI.Page  
  3. Protected Sub Page_Load(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Load  
  4. If Not IsPostBack Then  
  5. If Session("FirstName"Is Nothing AndAlso Session("LastName"Is Nothing Then  
  6. Session("FirstName") = "Sarthak"  
  7. Session("LastName") = " Varshney"  
  8. lblString.Text = "Welcome " & Convert.ToString(Session("FirstName")) & Convert.ToString(Session("LastName"))  
  9. Else  
  10. lblString.Text = Convert.ToString(Session("FirstName")) & " " & Convert.ToString(Session("LastName"))  
  11. lblfName.Text = Session("FirstName").ToString()  
  12. lbllName.Text = Session("LastName").ToString()  
  13. End If  
  14. End If  
  15. End Sub  
  16. End Class 

Ezmata Technologies Pvt Ltd
You manage your core business, while we manage your Infrastructure through ITaaS. It’s a game chang