Monday 29 July 2013

ASP.NET Session State Example in C#, VB.NET || What is Session in .NET || How to use Session in .NET ?

Hi friends, in this article I will explain about what is session? How to use session in ASP.NET?
Session : Server Side objects for saving your data for the session you are using the site, there is a time for which these objects stay in the memory and that is defined in web.config for session state section otherwise 20 mins by default.

http://support.microsoft.com/kb/307598
Session variables are stored in a SessionStateItemCollection object that is exposed through the HttpContext.Session property. In an ASP.NET page, the current session variables are exposed through the Session property of the Page object.
I will explain the session with some examples.
  • Gmail, facebook , yahoo sites are the examples of which websites are using sessions. Once we log in in these sites our log in information will not be lost up to be we logged out or close the system. Because they maintaining the sessions for the users.
  • Sessions are used to stored data on server side.
  • Cookies are used to store the data in client side. Browsers are example to cookies.
  • Web applications are stateless, in these web pages a new instance of a web page class is re-created each time the page is posted to the server.
  • Suppose the user enters the values and once he move to the next page, that values will be lost and that user would not be able to retrieve that data.
  • But we need to store the data on server. For those situations sessions will be used.


The following figure shows how session will work.

Syntax for Session:

In VB.NET:
Session["Username"] = uname.Text;
Session["Password"] = pswd.Text;
In C#:
Session["Username "] = uname.Text;
Session["Password "] = pswd.Text;

I will give the one example to how the session will be maintained in two web pages.
Create one website (file-> new-> website)
In that website create one web page name as Sessionfirstpage.aspx in that create page like below. 

or write the following code in source view.



<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>first  Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
        <asp:TextBox ID="uname" runat="server"></asp:TextBox>      
        </div>
        <div>
         <asp:Label ID="Label2" runat="server" Text="Real name"></asp:Label>
        <asp:TextBox ID="rname" runat="server"></asp:TextBox>&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Button" />
        </div>
    </form>
</body>
</html>


Then double click on button it will like below.

Partial Class Sessionfirstpage
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    End Sub
End Class

Imports system namespace.
Imports System

And write following in button click. That means creating session.
 In VB.NET:
 Session("username") = uname.Text
  Session("realname") = rname.Text
  Response.Redirect("sessionsecondpage.aspx")
In C#:
Session("username") = uname.Text;
Session("realname") = rname.Text;
Response.Redirect("sessionsecondpage.aspx");

Create one web page name as Sessionsecondpage.aspx in that create page like below.
or write the below code.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Second Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Username" ></asp:Label>
        <asp:Label ID="uname" runat="server" Text="Label" ForeColor="red"></asp:Label><br />
    </div>
        <div><asp:Label ID="Label3" runat="server" Text="Realname"></asp:Label>
        <asp:Label ID="rname" runat="server" Text="Label" ForeColor="red"></asp:Label></div>
    </form>
</body>
</html>


Partial Class sessionsecondpage
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
End Class

Then double click on page it will like below and write following in page load. That means using session.
 In VB.NET:
Partial Class sessionsecondpage
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        uname.Text = Session("username")
        rname.Text = Session("realname")
    End Sub
End Class
In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
partial class sessionsecondpage : System.Web.UI.Page
{
      protected void Page_Load(object sender, System.EventArgs e)
      {
            uname.Text = Session["username"];
            rname.Text = Session["realname"];
      }
    Public sessionsecondpage()
      {
            Load += Page_Load;
      }

}
  
We run the Sessionfirstpage.aspx page then the output is

Enter username and real name and click button.



It will redirect to SessionSecondpage.aspx and display the sessions.



I think you like my blog Aspdotnet-Roja why are waiting following me on facebook fan page Aspdotnet-roja

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.