Remove an item from session
Asp.net session state allows us to store and retrieve values for a user
as the user navigates asp.net pages in a website. HTTP is a stateless
protocol. Session state makes an asp.net application stateful that allows us
to save, update, remove, and read user data.
We can delete an item from the session state collection by using HttpSessionState Remove() method as Session Remove(). This Session Remove(name) method requires passing a parameter. This parameter name is 'name' and its data type is String. The Remove() method exists under System.Web.SessionState namespace.
The 'name' parameter value is the name of the item to delete from the session state collection. If the session state collection does not contain an element with the specified name (that passes by parameter), the collection remains unchanged. No exception is thrown.
The following asp.net c# example code demonstrates to us how can we remove an item from the session state collection. In this example first, we created three session variables and displayed output on a web browser. Next, we delete an item from the session state collection. And finally, we display the output on a web browser that indicates an item removed from session state collection.
We can delete an item from the session state collection by using HttpSessionState Remove() method as Session Remove(). This Session Remove(name) method requires passing a parameter. This parameter name is 'name' and its data type is String. The Remove() method exists under System.Web.SessionState namespace.
The 'name' parameter value is the name of the item to delete from the session state collection. If the session state collection does not contain an element with the specified name (that passes by parameter), the collection remains unchanged. No exception is thrown.
The following asp.net c# example code demonstrates to us how can we remove an item from the session state collection. In this example first, we created three session variables and displayed output on a web browser. Next, we delete an item from the session state collection. And finally, we display the output on a web browser that indicates an item removed from session state collection.
SessionRemove.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
Session["EmployeeID"] = "11";
Session["EmployeeName"] = "Jenny Jones";
Session["City"] = "Rome";
Label1.Text = "Session read...<br />";
Label1.Text += "Employee ID :" + Session["EmployeeID"];
Label1.Text += "<br />Employee Name :" + Session["EmployeeName"];
Session.Remove("EmployeeName");
Label1.Text += "<br /><br />Now remove the item [EmployeeName]";
Label1.Text += "<br />Employee ID :" + Session["EmployeeID"];
Label1.Text += "<br />Employee Name :" + Session["EmployeeName"];
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net session Remove example: how to remove an item from session</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">asp.net session example: Remove</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="DeepPink"
>
</asp:Label>
</div>
</form>
</body>
</html>