Pages

Subscribe:

Ads 468x60px

Saturday, March 10, 2012

how to get number of active visitor's online in asp.net using c#

In this web programming tutorial we will learn how to get number of active visitor's online in asp.net web site. Whenever distinct visitor opens the web site in his browser, new session is created for him. Here, I have used global HttpApplicationState class instance. HttpApplicationState class used to manage and share multiple sessions with in an ASP.NET web site. Open Global.aspx file from your web Application project, if it file does not exists, right click on your web application in the Solution Explorer, and select Add New Item -> Global Application Class. Following is the script of Global Application Class.
void Application_Start(object sender, EventArgs e)
    {
        Application["onlineUsers"] = 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)
    {
        Application.Lock();
        Application["onlineUsers"] = (int)Application["onlineUsers"] + 1;
        Application.UnLock();
    }
 
    void Session_End(object sender, EventArgs e)
    {
        Application.Lock();
        Application["onlineUsers"] = (int)Application["onlineUsers"] - 1;
        Application.UnLock();
    }
 

No comments:

Post a Comment