Retrieving And Storing Values From Session Using VB.NET
Here, we will see how to Storing and Retrieving values from Session.
Session
Here, we will see how to Storing and Retrieving values from Session. A session is defined as the period of time that a unique user interacts with a Web application. HTTP is a stateless protocol, it can't hold the client information on page. If user inserts some information, and move to the next page, that data will be lost and user would not able to retrieve the information. So what we need? we need to store information. Session provides that facility to store information on server memory.
Now creating a web application with two form. one for storing value of session and other for retrieving session value.
Storing a value to session
Now drag and drop a TextBox and a button control on the form.
.ASPX code
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
Now double click on the Button control and add the following code.
.cs code
Public Class sessionstore
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("UserName") = TextBox1.Text
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Response.Redirect("sessionretrive.aspx")
End Sub
End Class
Retrieve session value
Now Taking another form to retrieve session value. Drag a label control on the form and add the following code on the page load event.
Public Class sessionretrive
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("UserName") IsNot Nothing Then
Label1.Text = "Welcome : " + Session("UserName")
Else
End If
End Sub
End Class
Now run the application and test it.
Now enter the name in the TextBox.
Now click on the Button. TextBox value will be display on the label control.