ARTICLE

Storing and Retrieving values from Session in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET August 22, 2011
Here, we will see how to Storing and Retrieving values from Session.
Reader Level:

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.

session1.gif

Figure1

Now enter the name in the TextBox.

session2.gif

Figure2

Now click on the Button. TextBox value will be display on the label control.

session3.gif

Figure3

Login to add your contents and source code to this article
share this article :
post comment
 

Good article for Beginners .Rohatash

Posted by Dea Saddler Aug 26, 2011
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor