How to use Session in with Example


How to use Session in with Example

Session in with C#

In previous article we learned about session, session id, session state types in In the following example we will learn how to use session state to store and retrieve values of a user. Find the C# code below :-

Related Articles
session in c# with example,session in with example code project,view state in with example,session in using c#,session in asp net using c sharp,cookies in asp net example
Session in asp net using c sharp
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html> 
<html xmlns="">
<head runat="server">
    <title>Session in asp net using c sharp</title>
    <form id="form1" runat="server">
            <h3>How to use session in with example</h3>
                Enter Userame:<br />
                <asp:TextBox ID="txtUserName" runat="server" MaxLength="50"></asp:TextBox><br />
                <br />
                <asp:Button ID="btnCreateSession" runat="server"
                    Text="Click to Create Session" OnClick="btnCreateSession_Click" />
                    <asp:Button ID="btnRetrieveSession" runat="server" CausesValidation="false"
                        Text="Click to Retrieve Session Value" OnClick="btnRetrieveSession_Click" />
                    <asp:Button ID="btnRemoveSession" runat="server" CausesValidation="false"
                        Text="Click to Remove Session Value" OnClick="btnRemoveSession_Click" />
                    <asp:Button ID="btnRemoveAll" runat="server" CausesValidation="false"
                        Text="Click to Remove All Sessions" OnClick="btnRemoveAll_Click" />
                <b>Note</b>: First create a session by providing user name in text field, then you can retrieve the value from session.
                Value stored in Session:
                        <asp:Label ID="lblSessionValue" ForeColor="OrangeRed" runat="server"></asp:Label></strong>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    protected void btnCreateSession_Click(object sender, EventArgs e)
        Session["Username"] = txtUserName.Text.Trim();
        txtUserName.Text = string.Empty;

    protected void btnRetrieveSession_Click(object sender, EventArgs e)

    protected void btnRemoveSession_Click(object sender, EventArgs e)


    protected void btnRemoveAll_Click(object sender, EventArgs e)


    private void DisplaySessionValue()
        if (Session["Username"] != null)
            lblSessionValue.Text = Convert.ToString(Session["Username"]);
            lblSessionValue.Text = "No Value has been stored in session";

I hope you will enjoy the development tips while programming with using C#. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. Also If you like this article, don't forget to share this article with your friends and colleagues.
How to use Session in with Example Reviewed by Ravi Kumar on 1:21 AM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form


Email *

Message *

Powered by Blogger.