Reputation: 873
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
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
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
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:
int
rather than a string
.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
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
Upvotes: 1