z
 
:: Home     :: MS Dynamics CRM     :: .Net 1.1     :: .Net 2.0     :: Sharepoint Portal     :: Ajax

  login:        
  passwords:  
 

Resources

Finding Correct Content Managemet System
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Workflow Managemet Systems
Workflow management is a crucial component in organizing a variety of business processes so that they benefit the business as a whole and increase profitability...

Using the Power of Content Management Systems
With page editors that resemble a word processor program, adding content with a CMS interface is simple and fun. Most CMS software also allows you to change the location of your content pages and links easily, while the back end processes takes care of updating the links throughout your site...

Content Management Systems (CMS): What They Are And Why We Love Them
In the past, individuals who took interest in having and operating their own websites were burdened with the task of learning HTML, DHTML, and other web-based technologies such as JavaScript and CSS. The only alternative to this was, unfortunately, to pocket the expenses and costs required to pay a web developer to build and maintain it for them...

Outsourcing
Post your project for outsourcing and get bids from qualified programmers, designers, interpreters, copywriters.


 

Code Walkthroughs

Datagrid Formatting the Data
We are able to format the content of the datagrid cell by one of two simple methods, dependant upon whether the column is a bound column or whether it is a template column. In our example we shall format the column to have to digits after the decimal point , followed by a...

Datagrid Highlight a Row With Click Through
It is relatively easy to add alternating colours to the rows in your datagrid. However, when we move the mouse over the rows we may want to highlight this row, and possibly to add the option of a click through based on the row selected...

Add a Delete Button to a Datagrid
To add a delete button to a datagrid follows a similar process to adding an edit button. In the datagrid header...

Add an Edit Button to a Datagrid
The datagrid has a predefined editColumn for handling the editing of a datagrid. Adding this simple column definition to a datagrid adds a powerful feature. When a row is not in edit mode the column item shows the word...

Making a Datagrid Row Editable
Two of the most popular methods of editing a datagrid in asp.net are to either select the row and take the user off to a different presentation of the data, or to change the formatting of the row presented in the database with appropriate edit text boxes, checkboxes and...

Adding Tooltips to Datagrid Rows
Adding tooltips to datagrid rows is easy, assuming that you have already created the code for adding row highlighting. In this article I shall assume that you have already read the article entitled Datagrid Highlight a Row With Click Through...

Binding a Datagrid to an Access Database
This list covers the full lifecycle of a content management system, from initially creating the content, through to delivering it to end users...

Adding Data to a DropDownList
The aim of this article is to answer the question 'How do I add items to a DropDownList?' Initially as part of the declaration for the DropDownList we can also define a number of items, much in the same way as in classic ASP...

Getting Current Date Time
In classic ASP we had now() which would return the current date and time. For asp.net this no longer exists. So what should we use...

Test if File Exists
Sometimes, in order to reduce our chance of error, when working with the filesystem in ASP.NET, we need to determine wether a file exists before performing an action on it. The following short piece of code will enable us to test whether a file exists...

Using Javascript with ASP.NET Form Elements
Adding simple pieces of Javascript to an Asp.net page can be acheived by adding to the attributes of the particular imagebutton or linkbutton. if its normal ASP.Net Button then you can...

Regular Expressions
In the table below we list the characters used in .Net regular expressions, together with their meaning, But first...

Authentication in Asp.net
Forms authentication in ASP.Net is far more easier and safe than Asp 3. It is possible to place a web.config file in any directory of a web site.Therefore, we are able to make most of a web site public, whilst providing authentication on, say, one directory...

Discussion Forums

General ASP.NET

.Net Programming

cSharp Home

Sql Server Home

Javascript / Client Side Development

IT Jobs

Ajax Programming

Ruby on Rails Development

Perl Programming

C Programming Language

C++ Programming

Python Programming Language

Laptop Suggestions?

TCL Scripting

Fortran Programming

Scheme Programming

32. Session

FAQ Home
   32.1 How to print out all the variables in the Session?
   32.2 Why isn't session state available?
   32.3 Can I share session state between ASP.NET and ASP pages?
   32.4 Can I share session state between web applications (i.e. "virtual directories" or "applications" in IIS)?
   32.5 What is the difference between Session.Abandon() and Session.Clear()?
   32.6 The SessionID remains the same even after the Session times out or Session abandons?
   32.7 Why are my Session variables lost frequently when using InProc mode?
   32.8 Does session state have a locking mechanism that serialize the access to state?
   32.9 Response.Redirect and Server.Transfer is not working in Session_End?
   32.10 Is the session Timeout attribute a sliding timeout value?
   32.11 I am writing my own HttpHandler. Why is session state not working?
   32.12 Why isn't Session available in my event handlers in global.asax?
   32.13 When I use the Session in component class as Session("CustID"), I get error message "Session is not declared". Why?
   32.14 When I create a Session variable if I'm using inproc, where is the session variable stored?
   32.15 Is the session timeout attribute in minutes or seconds?
   32.16 Will my session state be saved when my page hit an error? Will my session state be saved when my page hit an error? In Session_End, I tried to do some cleanup job using SQL but it failed. Why?
   32.17 Why do I lose my Session variables on redirect when I set "cookieless" to true?
   32.18 How to store SortedList in Session or Cache?
   32.19 Why do I get the error message "Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive"?
   32.20 How to remove a Session variable?
   32.21 Is there any way to know how much memory is being used by session variables in my application?
   32.22 I have a frameset page which has an HTM extension, and I found out that each frame it contains displays a different session id on the first request. Why?
   32.23 Can different apps store their session state in different databases on the same SQL server?
   32.24 Do I have a valid HttpSessionState object and HttpContext object in Session_End?
   32.25 Why aren't my sessions expiring, I am using SQLServer mode?
   32.26 I set EnableSessionState to "ReadOnly", but in InProc mode I can still modify the session. Why is that?
   32.27 How can I avoid specifying a plain password for my sql connection?
   32.28 How can I use Session variables in a class?
   32.29 What kind of objects can I store in a Session State?
   32.30 Why did my request hang after I switch to SQLServer mode?
   32.31 What are the disadvantages of setting cookieless to true?
   32.32 Can I store my session state in a database other than tempdb, In SqlServer mode?

32.1 How to print out all the variables in the Session?


VB.NET


Dim strKey as string
For Each strKey In Session.Keys
Response.Write(strKey + " : " + Session(strKey).ToString() + "<br>")
Next


C#


foreach (string strKey in Session.Keys)
{
Response.Write(strKey + " : " + Session[strKey].ToString() + "<br>");
}



32.2 Why isn't session state available?


  • - First, check your web.config, machine.config and your page directive to make sure you have enabled session state. Reference:
  • - session state is not available just everywhere, anytime. It is available only after the HttpApplication.AcquireRequestState event is called. For example, it is NOT available in the Application_OnAuthenticateRequest handler inside global.asax. For details, see: Handling Public Events
  • - Lastly, make sure System.Web.SessionState.SessionStateModule is included the in your config files. A common case is that SharePoint application will remove this module from their web.config files (for performance reason), and thus session state isn't available.

 


32.3 Can I share session state between ASP.NET and ASP pages?


No. This MSDN article shows how to work around it: How to Share Session State Between Classic ASP and ASP.NET


32.4 Can I share session state between web applications (i.e. "virtual directories" or "applications" in IIS)?


No.


32.5 What is the difference between Session.Abandon() and Session.Clear()?


The major difference is that if you call Session.Abandon(), Session_End will be fired (for InProc mode), and in the next request, Session_Start will be fired. Session.Clear( ) just clears the session data without killing it.


32.6 The SessionID remains the same even after the Session times out or Session abandons?


The SessionID lasts as long as the browser session lasts even though the session state expires after the indicated timeout period i.e the same session ID can represent multiple sessions over time where the instance of the browser remain the same.


32.7 Why are my Session variables lost frequently when using InProc mode?


This can be due to application recycle.
See PRB: Session variables are lost intermittently in ASP.NET applications
In v1, there is also a bug that will cause worker process to restart. It's fixed in SP2 and v1.1. See FIX: ASP.NET Worker Process (Aspnet_wp.exe) Is Recycled Unexpectedly.


32.8 Does session state have a locking mechanism that serialize the access to state?


Session state implements a reader/writer locking mechanism:

  • A page (or frame) that has session state write access (e.g. <%@ Page EnableSessionState="True" %>) will hold a writer lock on the session until the request finishes.
  • A page (or frame) that has session state read access (e.g. <%@ Page EnableSessionState="ReadOnly" %>) will hold a reader lock on the session until the request finishes.
  • Reader lock will block a writer lock; Reader lock will NOT block reader lock; Writer lock will block all reader and writer lock.
  • That's why if two frames both have session state write access, one frame has to wait for the other to finish first

 


32.9 Response.Redirect and Server.Transfer is not working in Session_End?


Session_End is fired internally by the server, based on an internal timer. And thus there is no HttpRequest associted when that happens. That is why Response.Redirect or Server.Transfer does not make sense and will not work.


32.10 Is the session Timeout attribute a sliding timeout value?


The session Timeout is a sliding expiration time, meaning whatever your page access session state, the expiration time will be moved forward. Note that as long as a page has NOT disabled session state, it will access the session automatically when requested.


32.11 I am writing my own HttpHandler. Why is session state not working?


Your HttpHandler has to implement the "marker" interface IRequiresSessionState or IReadOnlySessionState in order to use session state.


32.12 Why isn't Session available in my event handlers in global.asax?


It depends on which event you're handling. Session is available only after AcquireRequestState event


32.13 When I use the Session in component class as Session("CustID"), I get error message "Session is not declared". Why?


Use HttpContext.Current.Session i.e
VB.NET


HttpContext.Current.Session("CustID") = "1"


C#


HttpContext.Current.Session["CustID"] = "1";



32.14 When I create a Session variable if I'm using inproc, where is the session variable stored?


  • In II5, it's stored in the memory of aspnet_wp.exe.
  • For IIS6, by default all apps will share the same application pool, i.e. the session state is stored in the memory of the process w3wp.exe. They are NOT separated per application, but instead per application pool (w3wp.exe)

 


32.15 Is the session timeout attribute in minutes or seconds?


The Timeout setting is in minutes, not in seconds. i.e The timeout attribute specifies the number of minutes a session can be idle before it is abandoned. The default is 20


32.16 Will my session state be saved when my page hit an error? Will my session state be saved when my page hit an error? In Session_End, I tried to do some cleanup job using SQL but it failed. Why?


  1. Session_End is supported only in InProc mode.
  2. Session_End is run using the account which runs the worker process (aspnet_wp.exe), which can be specified in machine.config. Therefore, in your Session_End, if you connect to SQL using integrated security, it will use that worker process account credential to connect, and may fail depending on your SQL security settings.

 


32.17 Why do I lose my Session variables on redirect when I set "cookieless" to true?


When using cookieless, you must use relative path (e.g. ..\webform1.aspx) instead of absolute path (e.g. \dir1\subdir1\webform1.aspx). If you use absolute path, ASP.NET cannot preserve your session ID in the URL.


32.18 How to store SortedList in Session or Cache?


VB.NET


Dim x As New SortedList()
x.Add("Key1", "ValueA")
x.Add("Key2", "ValueB")


To store in Session:


Session("SortedList1") = x


and to retrieve


Dim y As SortedList = CType(Session("SortedList1"), SortedList)


C#


SortedList x = new SortedList();
x.Add("Key1", "ValueA");
x.Add("Key2", "ValueB");


To store in Session:


Session["SortedList1"] = x;


and to retrieve


SortedList y = (SortedList) Session["SortedList1"];



32.19 Why do I get the error message "Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive"?


This problem may occur after you install Windows Sharepoint Server(WSS) on a server that has Microsoft Visual Studio .NET 2003 installed. The WSS ISAPI filter handles all incoming URLs. When you browse one of the ASP.NET Web application virtual directories, the ISAPI filter does not locate the URL path of the folder.
To resolve this refer Session state cannot be used in ASP.NET with Windows SharePoint Services


32.20 How to remove a Session variable?


Use HttpSessionState.Remove()


32.21 Is there any way to know how much memory is being used by session variables in my application?


No


32.22 I have a frameset page which has an HTM extension, and I found out that each frame it contains displays a different session id on the first request. Why?


The reason is that your frameset page is an HTM file instead of an ASPX file.
In normal a scenario, if the frameset is an aspx file, when you request the page, it will first send the request to the web server, receive an asp.net session cookie (which holds the session id), and then the browser will send individual requests for the frames, and each request will carry the same session id.
However, since your frameset page is an htm file, the first request comes back without any session cookie because the page was serviced by ASP and not ASP.NET. Then again your browser sends out individual requests for each frame. But this time each individual request will NOT carry any session id, and so each individual frame will create its own new session. That's why you will see different session ids in each frame. The last request that comes back will win by overwriting the cookie written by the previous two requests. If you do a refresh, you will see them having the same session id.
This behavior is by-design, and the simple solution is to change your frameset page to .aspx.


32.23 Can different apps store their session state in different databases on the same SQL server?


Yes. Refer FIX: Using one SQL database for all applications for SQL Server session state may cause a bottleneck


32.24 Do I have a valid HttpSessionState object and HttpContext object in Session_End?


You will have the HttpSessionState object available. Just use 'Session' to access it.
For HttpContext, it is not available because this event is not associated with any request.


32.25 Why aren't my sessions expiring, I am using SQLServer mode?


In SQLServer mode, session expiration is carried out by the SQL Agent using a registered job. Make sure your SQL Agent is running.


32.26 I set EnableSessionState to "ReadOnly", but in InProc mode I can still modify the session. Why is that?


Even those enableSessionState is marked as ReadOnly, but in InProc state, the user can still modify the session. The only difference is that the session will not be locked during the request. This limitation is by-design


32.27 How can I avoid specifying a plain password for my sql connection?


Include a sql trusted connection, or put the connection string as encrypted data in the registry. Refer

 


32.28 How can I use Session variables in a class?