Sheetal
Sheetal

Reputation: 873

Count Number of Visitors in WebSite using ASP.Net and C#

I want to keep track of the number of visitors to my site.

I tried the following code in the Global.asax class,

<script runat="server">

  public static int count = 0;
  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;
  }

</script>

I am retrieving the value in the aspx page as follows:

protected void Page_Load(object sender, EventArgs e)
{
  int a;
  a = Convert.ToInt32((Application["myCount"]));
  Label4.Text = Convert.ToString(a);
  if (a < 10)
    Label4.Text = "000" + Label4.Text ;
  else if(a<100)
    Label4.Text = "00" + Label4.Text;
  else if(a<1000)
    Label4.Text = "0" + Label4.Text;
}

The above coding works fine. It generates the Visitors properly but the problem is when I restart my system, the count variable again starts from 0 which logically wrong.

I want the value of count to be incremented by 1 from the last count value.

So can anyone tell me how to accomplish this task?

Please help me out! Thanks in advance!

Upvotes: 7

Views: 46141

Answers (4)

Dinesh Rabara
Dinesh Rabara

Reputation: 1127

C# code is show below:

    protected void Page_Load(object sender, EventArgs e)
    {
        this.countMe();
    enter code here
        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

        lblCounter.Text = tmpDs.Tables[0].Rows[0]["hits"].ToString();
    }

    private void countMe()
    {
        DataSet tmpDs = new DataSet();
        tmpDs.ReadXml(Server.MapPath("~/counter.xml"));

        int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());

        hits += 1;

        tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();

        tmpDs.WriteXml(Server.MapPath("~/counter.xml"));

    } 

Then you need to have an xml file in the root directory to make the code work as well. The XML file will look like this:

<?xml version="1.0" encoding="utf-8" ?>
<counter>
  <count>
    <hits>0</hits>
  </count>
</counter>

Upvotes: 5

sanath rohilla
sanath rohilla

Reputation: 21

In First Answer U had declare count variable globally,that's why in every new session count starts with 0.for better result ,increment application[] variable inside session_start method.

Upvotes: 2

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26956

If you want the count to keep incrementing over application restarts, you'll need to store the value somewhere - in a database or a file somewhere, and load that value up when the application starts.

Also, you can use the following to ensure your displayed count is always at least 4 characters:

int a;
a = Convert.ToInt32(Application["myCount"]);
Label4.Text = a.ToString("0000");

See Custom Numeric Format Strings for more info.


Edit to respond to comment

Personally, I'd recommend using a database over writing to the file system, for at least the following reasons:

  1. Depending on your host, setting up a database may well be a lot easier than enabling write access to your file system.
  2. Using a database will allow you to store it as an int rather than a string.
  3. Under heavy traffic, you'll have issues with multiple threads trying to open a text file for write access - which will cause a lock on the file, and cause a bottle neck you don't need.

Various resources will tell you how to connect to a database from your code, a good place to start would be this How To: Connect to SQL Server, and looking into the methods under "What are the alternatives" for details on how to query and update the database.

Upvotes: 7

Arthur
Arthur

Reputation: 8129

Usually you use other Tools for that Task (weblog analyser).

As you store your value in Memory (Application["myCount"]) this value will not survive a server restart. So you have to store it in a

  • database
  • plain textfile
  • whatever

Upvotes: 1

Related Questions