473,378 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. <%@ Application Codebehind="Global.asax.cs" Inherits="WebApplication1.Global" Language="C#" %>
  2. <object runat="server" scope="application" ID="MyInfo" 
  3.     PROGID="MSWC.MYINFO">
  4. </object>
  5.  
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="Global.asax.cs" Inherits="WebApplication1.Global" Language="C#" %>
Line 2: <object runat="server" scope="application" ID="MyInfo"
Line 3: PROGID="MSWC.MYINFO">
Line 4: </object>


Source File: /global.asax Line: 2


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082

Please help.

Thanks.
Dec 13 '09 #1
3 3278
sanjib65
102 100+
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 HttpApplicationState 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:

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(Object sender, EventArgs e)
  2. {
  3.     Label1.Text = MyInfo.Title;
  4. }
  5.  
Have you tried that?
Dec 13 '09 #2
I want to use object in Global.asax:

Expand|Select|Wrap|Line Numbers
  1. <object runat="server" scope="application" ID="MyInfo" 
  2.     PROGID="MSWC.MYINFO">
  3. </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?
Dec 13 '09 #3
sanjib65
102 100+
Try this code in Global.asax :
Expand|Select|Wrap|Line Numbers
  1. <%@ Application Language="C#" %>
  2.  
  3. <script runat="server">
  4.  
  5.     void Application_Start(object sender, EventArgs e) 
  6.     {
  7.         // Code that runs on application startup
  8.         Application.Add("count", 0);
  9.         Application.Add("PageView", 0);
  10.     }
  11.  
  12.     void Application_End(object sender, EventArgs e) 
  13.     {
  14.         //  Code that runs on application shutdown
  15.  
  16.     }
  17.  
  18.     void Application_Error(object sender, EventArgs e) 
  19.     { 
  20.         // Code that runs when an unhandled error occurs
  21.  
  22.     }
  23.  
  24.     void Session_Start(object sender, EventArgs e) 
  25.     {
  26.         // Code that runs when a new session is started
  27.         try
  28.         {
  29.             int PageView = int.Parse(Application.Get("PageView").ToString());
  30.             PageView++;
  31.             Application.Set("PageView", PageView);
  32.         }
  33.         catch (Exception ex)
  34.         {
  35.             Response.Write(ex);
  36.         }
  37.         Session.Add("somevalue", Session.SessionID);
  38.     }
  39.  
  40.     void Session_End(object sender, EventArgs e) 
  41.     {
  42.         // Code that runs when a session ends. 
  43.         // Note: The Session_End event is raised only when the sessionstate mode
  44.         // is set to InProc in the Web.config file. If session mode is set to StateServer 
  45.         // or SQLServer, the event is not raised.
  46.         int PageView = int.Parse(Application.Get("PageView").ToString());
  47.         PageView--;
  48.         Application.Set("PageView", PageView);
  49.  
  50.     }
  51.  
  52. </script>
  53.  
And in Default.aspx.cs use this code:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12.  
  13. public partial class _Default : System.Web.UI.Page 
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         if (Page.IsPostBack == true)
  18.         {
  19.             Label1.Text = string.Format("Original: " + Application["count"]);
  20.             ViewState.Add("ViewStateItem", "Hello World" );
  21.         }
  22.         Label3.Text = "Page Viewed so far...." + Application.Get("PageView").ToString();    
  23.         //Response.Write("Page Viewed so far...." + Application.Get("PageView").ToString());
  24.     }
  25.     protected void Button1_Click(object sender, EventArgs e)
  26.     {
  27.         int Count = (int)Application["count"];
  28.         Count++;
  29.         Application["count"] = Count;
  30.         Label2.Text = String.Format("Updated: {0}", Application["count"]);
  31.         //Label4.Text = ViewState["ViewStateItem"].ToString();
  32.         Label4.Text = Session["somevalue"].ToString();
  33.     }
  34. }
  35.  
In Default.aspx I used a button and 4 Labels whose text properties hold the Application and Session State values.
Dec 13 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: orekin | last post by:
Hi There I have been trying to come to grips with Application.Run(), Application.Exit() and the Message Pump and I would really appreciate some feedback on the following questions .. There are...
0
by: Brett | last post by:
I am working with vb.net in a asp.net application. I have created a config file in xml format. The goal is to be able to change the config file without having to recompile the entire...
3
by: Grinninger | last post by:
Hello, I am using ASP.NET, C# under W2K. I try to keep some important information in application- and session-state Objects. With the recycling of the ASPNet_WP.EXE my application- and...
1
by: Bruce W.1 | last post by:
I'm storing some things in the application state, like this: Application = "whatever"; In Visual Studio, when I shut down the web app then start it again the application state information...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
8
by: Anthony P. Mancini | last post by:
I'm working on a proof of concept that will ultimately be deployed on a load balancer. For the sake of a preliminary demonstration I created a C# object and marked it's attributes as Public...
2
by: Mel | last post by:
This may be a stupid question, but here goes... I have created a NameValueCollection in my website's application state. If, during a page request, I add a string key and string value to the...
6
by: Eric McVicker | last post by:
Session state has options to be inproc, state server or sql server. Why does Application state not allow for state server or sql server so the same Application state could be shared between...
6
by: spacehopper_man | last post by:
I'm considering ditching all use of Session state in favour of Application state. This is because - from what I can work out - it will be more memory efficient for me. I have three questions:...
0
by: =?Utf-8?B?SkhhbGV5?= | last post by:
Our system is: IIS Server: dual Intel Xeon 2.80 GHz, 4 GB Ram Windows Server 2003 SP2 IIS 6.0 SQL Server: dual Intel Xeon 2.80 GHz, 4 GB Ram (separate server) Windows Server 2003 SP2 SQL...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.