ASP.NET Application State

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qwedster
    New Member
    • Jul 2008
    • 24

    ASP.NET Application State

    Hello!

    I tried to use Application State as given in MSDN (http://msdn.microsoft.com/en-us/library/ms178594.aspx):

    I opened a WebApplicaion (C#) in Visual Studio 2008 and added Global.asax file.

    And added the following code (from MSDN) in Global.asax:

    Code:
    <%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication1.Global" Language="C#" %>
    <object runat="server" scope="application" ID="MyInfo" 
        PROGID="MSWC.MYINFO">
    </object>
    Just to check, when I run the application I get the following error:


    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    Parser Error
    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: Cannot load type with ProgID 'MSWC.MYINFO'.

    Source Error:


    Line 1: <%@ Application Codebehind="Glo bal.asax.cs" Inherits="WebAp plication1.Glob al" Language="C#" %>
    Line 2: <object runat="server" scope="applicat ion" ID="MyInfo"
    Line 3: PROGID="MSWC.MY INFO">
    Line 4: </object>


    Source File: /global.asax Line: 2


    --------------------------------------------------------------------------------
    Version Information: Microsoft .NET Framework Version:2.0.507 27.3603; ASP.NET Version:2.0.507 27.3082

    Please help.

    Thanks.
    Last edited by Frinavale; Dec 14 '09, 02:39 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • sanjib65
    New Member
    • Nov 2009
    • 102

    #2
    Application State

    I hope you have noticed this line in MSDN :

    "You can use application state in two ways. You can add, access, or remove values from the Contents collection directly through code. The HttpApplication State class can be accessed at any time during the life of an application. However, it is often useful to load application state data when the application starts. "

    You're going second way, no matter, but you need to access Application object through the Page_Load method by this code in any page:

    Code:
    protected void Page_Load(Object sender, EventArgs e)
    {
        Label1.Text = MyInfo.Title;
    }
    Have you tried that?

    Comment

    • qwedster
      New Member
      • Jul 2008
      • 24

      #3
      I want to use object in Global.asax:

      Code:
      <object runat="server" scope="application" ID="MyInfo" 
          PROGID="MSWC.MYINFO">
      </object>

      If I use this code, the I get Parser Error Message: Cannot load type with ProgID 'MSWC.MYINFO'.

      Can you give me the right code for Global.asax and Default.aspx.cs ?
      Last edited by Frinavale; Dec 14 '09, 02:40 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

      Comment

      • sanjib65
        New Member
        • Nov 2009
        • 102

        #4
        Applicaton State

        Try this code in Global.asax :
        Code:
        <%@ Application Language="C#" %>
        
        <script runat="server">
        
            void Application_Start(object sender, EventArgs e) 
            {
                // Code that runs on application startup
                Application.Add("count", 0);
                Application.Add("PageView", 0);
            }
            
            void Application_End(object sender, EventArgs e) 
            {
                //  Code that runs on application shutdown
        
            }
                
            void Application_Error(object sender, EventArgs e) 
            { 
                // Code that runs when an unhandled error occurs
        
            }
        
            void Session_Start(object sender, EventArgs e) 
            {
                // Code that runs when a new session is started
                try
                {
                    int PageView = int.Parse(Application.Get("PageView").ToString());
                    PageView++;
                    Application.Set("PageView", PageView);
                }
                catch (Exception ex)
                {
                    Response.Write(ex);
                }
                Session.Add("somevalue", Session.SessionID);
            }
        
            void Session_End(object sender, EventArgs e) 
            {
                // Code that runs when a session ends. 
                // Note: The Session_End event is raised only when the sessionstate mode
                // is set to InProc in the Web.config file. If session mode is set to StateServer 
                // or SQLServer, the event is not raised.
                int PageView = int.Parse(Application.Get("PageView").ToString());
                PageView--;
                Application.Set("PageView", PageView);
        
            }
               
        </script>
        And in Default.aspx.cs use this code:

        Code:
        using System;
        using System.Configuration;
        using System.Data;
        using System.Linq;
        using System.Web;
        using System.Web.Security;
        using System.Web.UI;
        using System.Web.UI.HtmlControls;
        using System.Web.UI.WebControls;
        using System.Web.UI.WebControls.WebParts;
        using System.Xml.Linq;
        
        public partial class _Default : System.Web.UI.Page 
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Page.IsPostBack == true)
                {
                    Label1.Text = string.Format("Original: " + Application["count"]);
                    ViewState.Add("ViewStateItem", "Hello World" );
                }
                Label3.Text = "Page Viewed so far...." + Application.Get("PageView").ToString();    
                //Response.Write("Page Viewed so far...." + Application.Get("PageView").ToString());
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                int Count = (int)Application["count"];
                Count++;
                Application["count"] = Count;
                Label2.Text = String.Format("Updated: {0}", Application["count"]);
                //Label4.Text = ViewState["ViewStateItem"].ToString();
                Label4.Text = Session["somevalue"].ToString();
            }
        }
        In Default.aspx I used a button and 4 Labels whose text properties hold the Application and Session State values.

        Comment

        Working...