Hi,

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

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

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; 
}

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!

You should do analysis on your log files to deteremine the number of visitors. Check out "smarter stats."

Most of the Paid domains give this for free in their Control Panels. or you can try this

protected void Page_Load(object sender, EventArgs e)
    {
        Application.Lock();
        if(Application["HitCount"]!=null)
         {
            Application["HitCount"]=(int)Application["HitCount"]+1;
         }
        else
         {
             Application["HitCount"] =1;
         }
         Application.UnLock();
        lblInfo.Text="The page has been accessed ("+Application["HitCount"].ToString()+") times";
    }

or this might Help

http://www.stardeveloper.com/articles/display.html?article=2002102501&page=1

Kind Regards

Vuyiswa Maseko

commented: neat, i didn't know about that +17

when you restart your system at that time IIS get close.Because of that all the information of users stored is get vanished.you have to use lock for that purpose.

Thanks a lot Vuyiswa Maseko

hi dear
You can also store count in another variable and make check the variables by applying condition code "if".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.