473,785 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET Application State

24 New Member
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="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.
Dec 13 '09 #1
3 3297
sanjib65
102 New Member
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:

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
qwedster
24 New Member
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 New Member
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
20087
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 quite a few words in this post but the questions are actually quite similar and should be fairly quick to answer ... (1) What is Happening with the Threads
0
1810
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 application. So when the Application starts I read the xml file and place each key-value pair into a Synchronized hash table. Now I want to save this hashtable to Application state for quick access and also so I don't have to open the config file a...
3
4308
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 session-state Objects get lost. Therfore I have some questions: -) Is it usual that the ASPNet_WP.EXE becomes recycled twice a day? -) What is the expected recyclingperiod for the ASPNet_WP.EXE ?
1
1365
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 remains. If there are no browsers using the app one would think that this would remove any application state, but it does not. However if I wait awhile then the application state information goes away.
4
3538
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 myVar as string, etc. It seems to me that the scope and duration are the same, as they both are there while the application is running, and both go away when it quits. I presume that one difference is that the application state can be "flushed," such...
8
1730
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 Shared Static. I also set the EnableSessions and EnableViewState Page directives to false. Here's the part that stumped me : as I was moving from page to page within the demo, I accidently realized the data I dropped
2
2013
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 collection, how are they stored? Strings are objects so I'm really only passing references, right? Now I assume that request handlers run in different threads with their own memory spaces, and when the request ends, the memory space is unaloted,...
6
1756
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 servers in a farm?
6
1761
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: 1) When is memory used for Session State freed (or essentially freed)? - if ever...
0
2215
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 Server 2000 We are having some problems with a website we are developing, and had some
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.