0% found this document useful (0 votes)
7 views4 pages

Session Object VB NET

The Session object in ASP.NET (VB.NET) is used to store and retrieve user-specific data on the server side, maintaining state across web pages for the same user. It allows adding, retrieving, and removing session data, with properties like SessionID and methods such as Clear and Abandon. Common use cases include login systems, shopping carts, and user preferences.

Uploaded by

Diya Ahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Session Object VB NET

The Session object in ASP.NET (VB.NET) is used to store and retrieve user-specific data on the server side, maintaining state across web pages for the same user. It allows adding, retrieving, and removing session data, with properties like SessionID and methods such as Clear and Abandon. Common use cases include login systems, shopping carts, and user preferences.

Uploaded by

Diya Ahuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Session Object in ASP.NET using VB.

NET

1. What is the Session Object in ASP.NET (VB.NET)?

--------------------------------------------------

The Session object allows you to store and retrieve user-specific data on the server side. It helps to maintain

state across web pages for the same user.

2. Adding Data to the Session Object

------------------------------------

Syntax:

Session("key") = value

Example:

Session("username") = "JohnDoe"

Session("loginTime") = DateTime.Now

3. Retrieving Data from the Session Object

------------------------------------------

Syntax:

Dim variable As DataType = CType(Session("key"), DataType)

Example:

Dim username As String = CType(Session("username"), String)

Safe Retrieval:

If Session("username") IsNot Nothing Then

Dim user As String = Session("username").ToString()

End If

4. Removing Data from the Session

---------------------------------

Remove a specific item:

Session.Remove("username")

Clear all session data:


Session.Clear()

Abandon the session:

Session.Abandon()

5. Session Variables

--------------------

Example:

Session("cartItems") = 5

Session("theme") = "dark"

6. Session Identifiers

-----------------------

Each session has a unique ID:

Dim sessionId As String = Session.SessionID

7. Properties and Methods of Session Object

-------------------------------------------

Properties:

- SessionID: Unique ID

- Count: Number of items

- IsNewSession: Indicates new session

- Timeout: Session timeout in minutes

Methods:

- Add(key, value): Add item

- Remove(key): Remove item

- Clear(): Remove all items

- Abandon(): End session

8. Full ASP.NET VB.NET Example (Web Forms)

------------------------------------------

Default.aspx:
<asp:Label ID="lblOutput" runat="server" />

<asp:Button ID="btnClear" runat="server" Text="Clear Session" OnClick="btnClear_Click" />

<asp:Button ID="btnAbandon" runat="server" Text="Abandon Session" OnClick="btnAbandon_Click" />

Default.aspx.vb:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

Session("username") = "Alice"

If Session("visitCount") Is Nothing Then

Session("visitCount") = 1

Else

Session("visitCount") = CType(Session("visitCount"), Integer) + 1

End If

Dim name As String = Session("username").ToString()

Dim visits As Integer = CType(Session("visitCount"), Integer)

lblOutput.Text = "Hello, " & name & ". You've visited " & visits.ToString() & " times."

End Sub

Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs)

Session.Clear()

lblOutput.Text = "Session cleared."

End Sub

Protected Sub btnAbandon_Click(ByVal sender As Object, ByVal e As EventArgs)

Session.Abandon()

lblOutput.Text = "Session abandoned."

End Sub

9. Real-Life Use Cases

-----------------------

- Login System

- Shopping Cart

- Theme Preference

- Form Wizard
10. Summary

-----------

- Persistent

- User-specific

- Server-side

- Easy to use

- Flexible

You might also like