Create HitCounter using ASP.Net and C#.Net
Hello Friends,
Some of our user writes to post the code for hit counter and i created a sample application.
Here i posted the code and sample application.
To do so follow the below steps :
Step 1 :
Open your project. then add one text file with name “hitcounter.txt”.
Step 2 :
Then open the page where you want to place the hit counter.
Step 3 :
Then Goto Page_Load event of that page. and put the below code.
Note : Here i used to put the code with Response.Write method. you can set the counter to a Label control. To show on Label Control use : Label1.Text=gethitcounts(); instead of Response.Write(gethitcounts());
Code :
using System.IO;
protected void Page_Load(object sender, EventArgs e)
{
Application.Lock();
Response.Write(gethitcounts()); //you can change here for label control
Application.UnLock();
}
public string gethitcounts()
{
string lastcount = "";
try
{
StreamReader SR = File.OpenText(Server.MapPath("hitcounter.txt"));
string getcount = null;
while ((getcount = SR.ReadLine()) != null)
{
lastcount = lastcount + getcount;
}
SR.Close();
long newcount = Convert.ToInt64(lastcount);
newcount++;
TextWriter TxtWtr = new StreamWriter(Server.MapPath("hitcounter.txt"));
TxtWtr.WriteLine(Convert.ToString(newcount));
TxtWtr.Close();
SR = File.OpenText(Server.MapPath("hitcounter.txt"));
getcount = null;
lastcount = "";
while ((getcount = SR.ReadLine()) != null)
{
lastcount = lastcount + getcount;
}
SR.Close();
}
catch (Exception ex)
{
TextWriter TxtWtr = new StreamWriter(Server.MapPath("hitcounter.txt"));
TxtWtr.WriteLine(Convert.ToString("1"));
TxtWtr.Close();
lastcount = "1";
}
return lastcount;
}
1. http://www.filesonic.com/file/1026929374/Hit_Counter.zip
2. http://www.fileserve.com/file/w7EcdZf
3. http://uploading.com/files/3ae87245/Hit_Counter.zip/
4. http://letitbit.net/download/47886.405cee85e9b5a71b90f8b92256c5/Hit_Counter.zip.html
Thank You.
Regards,
Manoranjan Sahoo
http://www.dotnetsquare.com

I have deployed the page to the client server and experienced Access denied error please help. Thanks!
Hello Master777,
you have to give permission to HitCounter.txt file as network write access permission. Because it always update the file for every hits.
Here’s a simple way of doing a hit counter with a database. Code is in C# and obviously it can be improved upon. This one will add a visitor even if the page is refreshed, like I said simple.
// Get the value stored in database
DataView count = (DataView)SqlDataSource4.Select(DataSourceSelectArguments.Empty);
DataRowView countView = count[0];
// Assign variable to value in database
int counter = (int)countView["Counter"];
//Get the page the user is currently on (Not Necessary)
string page = System.IO.Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]);
if (page == “Default.aspx” || page == “default.aspx”)
{
// Increment counter then update the database with the new value
counter++;
SqlDataSource4.UpdateParameters.Add(“count”, counter.ToString());
SqlDataSource4.UpdateCommand = “UPDATE HitCounter SET Counter = @count”;
SqlDataSource4.Update();
}
lblCounter.Text = counter.ToString();
Electricalbuyandsell.com