{"id":70,"date":"2006-04-21T12:09:22","date_gmt":"2006-04-21T11:09:22","guid":{"rendered":"http:\/\/www.galhano.com\/blog\/?p=70"},"modified":"2008-02-07T18:37:18","modified_gmt":"2008-02-07T17:37:18","slug":"aspnet-session-object-2","status":"publish","type":"post","link":"http:\/\/galhano.com\/blog\/?p=70","title":{"rendered":"ASP.NET Session Object"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" width=\"153\" height=\"48\" align=\"left\" alt=\"logo asp.net\" id=\"image55\" src=\"http:\/\/www.galhano.com\/blog\/wp-content\/2006\/04\/aspnet32.gif\" \/><font size=\"2\" face=\"Verdana\">ASP.NET provides a class called <font color=\"#ff0000\">HttpSessionState<\/font>, which is part of the namespace <font color=\"#ff0000\">System.Web.SessionState<\/font>.<\/p>\n<p>Just like Request and Response objects, an HttpSessionState object is also  created automatically for you in ASP.NET pages. This object is called &#8220;Session&#8221;.  You can access various methods and properties of this object.<br \/>\n<\/font><strong><font size=\"2\" face=\"Verdana\">What is in a session ?<\/font><\/strong><font size=\"2\" face=\"Verdana\"><\/p>\n<p>A session has several information including a <font color=\"#ff0000\">session ID<\/font>.  Some information is created and used by the server itself. But the most  important use of session is, it allows to store custom information.<br \/>\nYou can store any key-value pairs in session.<br \/>\n<\/font><\/p>\n<p><strong><font size=\"2\" face=\"Verdana\">How to store and retrieve values in ASP.NET  session ?<\/font><\/strong><\/p>\n<p><font size=\"2\" face=\"Verdana\">Just like you can store values in variables, you  can store values in session. Basically, you can assign values to session  variables. See the following example:<br \/>\nThe following code sample shows how to store a string value in session:<br \/>\n<font color=\"#ff0000\">    <\/font> <span style=\"background-color: #f2f2f2\"> Session(&#8220;UserId&#8221;) = &#8220;john&#8221;<\/span><\/p>\n<p>In the above sample, the value &#8220;john&#8221; is assigned to a session variable called &#8220;UserId&#8221;.  One important different here is, you do not declare the session variable as any  datatype. Just assign any value to any session variable. The session variables  will be automatically created when you simply assign values to them.<br \/>\n<\/font><strong><font size=\"2\" face=\"Verdana\">How long a session variable is  valid ?<\/font><\/strong><\/p>\n<p><font size=\"2\" face=\"Verdana\">A session variable retains it&#8217;s value as long as  the session is valid. Session variables are valid across all pages, as long as  you access all those pages from the same browser and within the timeout period.<\/p>\n<p>If you assign a value into a session variable from Page1.aspx and try to  retrieve it from Page2.aspx, it will work. But if you try to access the  Page2.aspx from another browser or close your original browser and open again,  the session variable would have lost it&#8217;s value because a new session is  created.<\/p>\n<p>What can be stored in  a session? <\/font><font size=\"2\" face=\"Verdana\">The above examples show storing and retrieving  simple strings in session. But you can store complex datatypes like Dataset also  into session. Many websites retrieve lot of user data from database and store  into session so that it can be accessed faster from memory (session) rather than  retrieving from database.<\/p>\n<p>However, it is not reccommded to store lot of data into session for large  websites. Session data is stored in memory and if there are thousands of online  users at the same time, server may run out of memory. <\/font><!--a7668aa8bd5599212a4f2c89c11d9284--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET provides a class called HttpSessionState, which is part of the namespace System.Web.SessionState. Just like Request and Response objects, an HttpSessionState object is also created automatically for you in ASP.NET pages. This object is called &#8220;Session&#8221;. You can access various methods and properties of this object. What is in a session ? A session has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"close","ping_status":"close","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-aspnet","author-admin"],"_links":{"self":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=70"}],"version-history":[{"count":0,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}