Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Saturday, 6 August 2016

Session State In Asp.Net With Examples

 No comments   

Session State 

Session State enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. It is one of the important state management variable in Asp.Net. It’s working on server side. 
Session provides a facility to store information on server memory. It can support any type of object to store along with our own custom objects. For every client, session data is stored separately, which means session data is stored on a per client basis.

Advantage of session state is that once you store your data in session and access that data from anywhere in the application across multiple pages. Advantage of session state over cookies is that session can store any type of data as well as complete dataset whereas cookies can store only small amount of data. Along with these advantages, some times session can cause performance issues in high traffic sites because it is stored in server memory and clients read data from the server. 

Write Session
:
Session["UserName"] = "Asphelps";

Read Session:

if (Session["UserName"] != null)
{
     string userName = Session["UserName"].ToString();
}

Session Events 

Session_Start
Session_End 
 


You can handle both these events in the global.asax file of your web application. When a new session initiates, the session_start event is raised, and the Session_End event raised when a session is abandoned or expires.

void Session_Start(object sender, EventArgs e) 
 {  
     // When a new session starts. Session variables can be initialised here 
 }
 void Session_End(object sender, EventArgs e) 
{  
      //When a session ends or time out. Used to free up per-user resources or log
        information  
}

 Clear Session Values

We can clear the session stored value by following three ways

1. Abandon - End user session in the application.

2. Remove -
Remove a particular session in the application.

3. Clear -
Clear all session object. 



Session.Remove("UserName");     // Remove UserName session
Session.Abandon();                        // End a user session
Session.Clear();                             // Clear all session items


Key Points
 
When you are using session then session will expire after specific time. This time is known as session timeout. Default session timeout value is 20 minutes.
But you can increase session timeout using session's TimeOut propery by declaring on page or in Web.Config.


<system.web>
    <sessionState timeout="60" />
</system.web>
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS Scope_Identity() Use of indexes comma seperated string in sql row_number() and dense_rank() salary table structure. while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write