In this article I will explain with an example, how to store (save) class object in Session in ASP.Net using C# and VB.Net.
Property Class
The class consists of following properties.
C#
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
VB.Net
Public Class Person
Public Property Name As String
Public Property Age As Integer
End Class
Storing class object in Session
Inside the Page Load event handler, an object of the Person class is created and values of Name and Age properties are set.
Finally, the person object is set in the Session.
C#
protected void Page_Load(object sender, EventArgs e)
{
Person person = new Person()
{
Name = "Mudassar Khan",
Age = 35
};
Session["Person"] = person;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim person As Person = New Person() With {
.Name = "Mudassar Khan",
.Age = 35
}
Session("Person") = person
End Sub
Screenshot
Downloads