How to remove an item from session in asp.net

Remove an item from session
asp.net session state allow us to store and retrieve values for a user as the user navigates asp.net pages in a web site. http is a stateless protocol. session state make an asp.net application stateful that allow us to save, update, remove and read users data.

we can delete an item from the session state collection by using HttpSessionState.Remove() method as Session.Remove(). this Session.Remove(name) method require to pass a parameter. this parameter name is 'name' and its data type is System.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 pass by parameter), the collection remain unchanged. no exception is thrown.

the following asp.net c# example code demonstrate us how can we remove an item from session state collection. in this example first we created three session variables and display output on web browser. next we delete an item from session state collection. and finally we display the output on web browser that indicate 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>
More asp.net examples