Application Object in ASP.NET using VB.NET

his article shows the application object in ASP.NET.
  • 1789

This article shows the application object in ASP.NET.

Application Object

To manage some information accessible in all the web forms for all the users. The Application object holds information that will be used by many pages in the application (like database connection information). The information can be accessed from any page. The information can also be changed in one place, and the changes will automatically be reflected on all pages.

- Use Lock() and Unlock() method of Application object to handle the dead locks. such as

Application.Lock()
Application["variable"]=value
Application.UnLock()

For example:

Create a visit counter for a web form.

Drag a control label on the form.

Form looks like this.

a1.gif
 

Figure 1.

Now double click on the form and add the following code.

VB code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim ctr As Integer

        If Application("vc") Is Nothing Then

            ctr = 0

        Else

            ctr = Convert.ToInt32(Application("vc"))

        End If

        ctr += 1

 

        Application.Lock()

        Application("vc") = ctr

        Application.UnLock()

 

        lblVisitCounter.Text = "You are visitor nummber : #" & ctr

    End Sub

 

C# code:

 

protected void Page_Load(object sender, EventArgs e)

        {  

            int vc = 0;

            if (Application["vc"] != null)

                vc = Convert.ToInt32(Application["vc"]);

 

            vc++;

 

            Application.Lock();

            Application["vc"] = vc;

            Application.UnLock();

 

            lblCounter.Text = "You are visitor number : " + vc;

        }

 

Now save and run the application.

a2.gif
 

Figure 2.

Now copy the address of the running window and open new window and paste the address of first window.

a3.gif
 

Figure 3.

The above example shows that visitor number will be increase every time when you open new window.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.