Click here to Skip to main content
14,767,160 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I want to display the number of Visitors currently viewing my website. If you know please help me on it.
Posted

Add a Global.asax file in your project from add new item menu.

In Global.asax handle the following events.

void Application_Start(object sender, EventArgs e)
    {
        Application["myCount"] = count;
        
    }



void Session_Start(object sender, EventArgs e)
    {
        count = Convert.ToInt32(Application["myCount"]); Application["myCount"] = count + 1;
        // Code that runs when a new session is started

    }



and in the page load of the page where you want to display the count

add the following code.

a = Convert.ToInt32((Application["myCount"]));
          Label1.Text = Convert.ToString(a);



the label will display the count

Remember if you reset the IIS the count will be reset.

So better to store it in database and get the data from backend and increment for every session creation and then save it in to database.
   
hi,

Use this code in global.asax.cs

public static int count = 0;
  void Application_Start(object sender, EventArgs e)
  {
    Application["userCount"] = count;
  }

  void Session_Start(object sender, EventArgs e)
  {
    count = Convert.ToInt32(Application["myCount"]);
    Application["userCount"] = count + 1;
  }


Get this userCount from Application Object & display on a page.
   
v2
Use global.ajax.cs file
You can do this by using the session event
session start add 1
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900