Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
 How to: Read Values from Applicatio...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
ASP.NET
How to: Read Values from Application State

Application state is a data repository that is available to all classes within an ASP.NET application. Application state is stored in memory on the server and is faster than storing and retrieving data in a database. Unlike session state, which is specific to a single user session, application state applies to all users and sessions. Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another.

Application state is stored in the HttpApplicationState class, a new instance of which is created the first time a user accesses a URL resource within an application. For more information, see ASP.NET Application State Overview.

Application state stores data typed as Object. Therefore, even though you do not have to serialize the data when storing it in application state, you must cast the data to the appropriate type when retrieving it. Although you can cast a null (Nothing in Visual Basic) object, if you attempt to use a non-existent application-state entry in some other way (for example, to examine its type), a NullReferenceException exception is thrown.

To read a value from application state

  • Determine whether the application variable exists, and then convert the variable to the appropriate type when you access it.

    The following code example retrieves the application state AppStartTime value and converts it to a variable named appStateTime of type DateTime.

    Visual Basic
    If (Not Application("AppStartTime") Is Nothing) Then
        Dim myAppStartTime As DateTime = _
            CDate(Application("AppStartTime"))
    End If
    

    C#
    if (Application["AppStartTime"] != null)
    {
        DateTime myAppStartTime = (DateTime)Application["AppStartTime"];
    }
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker