Subscribe

RSS Feed (xml)

Reading and Writing Values to the Application Object

You can store the name of the server in application state using


Application("ServerName") ="WebFarm1"


.You can retrieve it and display its value by


using MyLabel.Text=Application("ServerName").ToString(). Because it returns the


server name as an object, you need to convert it to a string using the .ToString()


method before assigning it to MyLabel.Text.The ASPX page is as follows:


<html>


<body>


<form id="Recipe0701vb" method="post" runat="server">


<asp:Label ID="MyLabel" Runat="server"/>


</form>


</body>


</html>


In <script runat="server" /> block or codebehind:


Sub Page_Load(sender As Object, e As EventArgs)


Application.Lock()


Application("ServerName")="WebFarm1"


Application.UnLock()


MyLabel.Text=Application("ServerName").ToString()


End Sub


Comments


Application state is useful for storing values that you need to access throughout your


application. Once a value is added to application state, it exists for the life of the application.


Because multiple pages can attempt to write to the Application collection simultaneously,


it must be locked using Application.Lock prior to any writes.The lock is


released using Application.UnLock.The only time locking isn't necessary is in code


that isn't executed by multiple page requests, such as in Application_Start in the


global.asax.

No comments:

Archives

Variety in the Web World