Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to display no of users visited(Not current online users) in my static asp.net website.






Currently I tried to display using global.asax file.

The code is in global.asax file like below:
protected void Application_Start(object sender, EventArgs e)
       {

           Application["OnlineUsers"] = 0;



       }

 protected void Session_Start(object sender, EventArgs e)
       {

           Application.Lock();
           Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
           Application.UnLock();



       }



But This code not working properly.Please any one do need full
Posted
Updated 27-Aug-12 0:37am
v2
Comments
pradiprenushe 27-Aug-12 6:49am    
What problem you are getting?
venkata chaitanya 27-Aug-12 7:42am    
Count is not showing properly.If i open my site on new machine,it is showing 1.If i open my site on another browser in same machine count is increasing.Again if i open my site on other machine(which is not in lan connection) again count is 1.

Have a look on following thread:
http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx[^]
Free hit counter[^]

Write code in global.asax file which interacts with the entire application.
The sample code is given below:
C#
void Application_OnStart(Object Sender, EventArgs E)
        {
            Application["CurrentUsers"] = 0;
        }
        void Session_OnStart(object Sender, EventArgs E)
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) + 1;
            Application.UnLock();
        }
        void Session_OnEnd(object Sender, EventArgs E)
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) - 1;
            Application.UnLock();
        }


Try this one:
global.asax file for that like
C#
void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["OnlineUsers"]=0;

    }

C#
void Session_Start(object sender, EventArgs e)
  {
      // Code that runs when a new session is started
      Session["Users"] =Convert.ToInt32( Application["OnlineUsers"])+ 1;
      Application["OnlineUsers"] = Session["Users"];
  }

page load:
C++
Response.Write(Session["Users"].ToString());
 
Share this answer
 
v3
Comments
venkata chaitanya 27-Aug-12 7:06am    
Here what will happen is when session ends the count will be decreased.But my requirement is for each new session visit count should increase.The visit count should increase until the application restarts.
For Example If 100 users visited my application in last week,I need to display the total count of visitors visited till this moment..
Prasad_Kulkarni 27-Aug-12 7:08am    
Store it in database and retrieve last value each time your page get accessed.
venkata chaitanya 27-Aug-12 7:15am    
I am not using any database.I do not have any log in screens.I just had static aspx page.
Prasad_Kulkarni 27-Aug-12 7:20am    
Then how can you get last week data?, you must have to store it somewhere, or else you can try simple free web counters the second link i have provided. It will work for sure. Have you had look on that?
venkata chaitanya 27-Aug-12 7:28am    
Application object still alive until application restarts ri8?(let us ignore the condition application restart.)I just want to increase the count for each session.I stored the value in application object.I mentioned the code in my question above.please correct my code.
in your login Screen

you can add below code

int Count=0
Application["OnlineUsers"] = Count+1;
 
Share this answer
 
Comments
venkata chaitanya 27-Aug-12 6:52am    
I dont have any log in screen.I just have home page.I need to display count of members visited in home page.
you need to write it XML or something, otherwise you lose total counts on any restart or anything.

Sample File opration to store counts(you can put it in Global.asax):
C#
public static int Visitors =0; //access this member whenever you need count.

  void Application_Start(object sender, EventArgs e) 
  {
    LogVistorCount();
  }

  void Session_Start(object sender, EventArgs e) 
  {
    LogVistorCount();
  }

 private void LogVistorCount()
 {
       string strXmlCreateSample = "" +
                                                    "<counter>" +
                                                        "<count>" +
                                                          "<hits>" +
                                                          "</hits>" +
                                                        "</count>" +
                                                    "</counter>";
      
      string filepath = Server.MapPath("~/VistorsCount.xml");
      try
      {
           DataSet tmpDs = new DataSet();
           
          if(!System.IO.File.Exists(filepath)
          {
             XmlDocument doc = new XmlDocument();
             doc.LoadXml(strXmlCreateSample);
             doc.Save(filepath);
          }
    
           tmpDs.ReadXml(filepath);
    
           int hits = Int32.Parse(tmpDs.Tables[0].Rows[0]["hits"].ToString());
    
           hits += 1;
           Visitors = hits;
           tmpDs.Tables[0].Rows[0]["hits"] = hits.ToString();
    
           tmpDs.WriteXml(filepath);
       }
       catch{}

  }
 
Share this answer
 
v2
Comments
venkata chaitanya 27-Aug-12 7:12am    
Yes,If server restarts we will loose data,Let us ignore this condition.I need to display correct count (let us take server wont restart or something else)

Thank you
Kuthuparakkal 27-Aug-12 7:16am    
You can always define a static member and increment it on each session/application start eg: public static int Visitors =0; You can always refer to this static member anytime.
Kuthuparakkal 27-Aug-12 7:25am    
You cannot ignore two things even if you ignore server reboot:
change to the web.config or when the application has no current users. The two condition will clear Application["OnlineUsers"]...

Thank You..
Kuthuparakkal
you can save a .txt file on server and u can save there the number of visitors?
 
Share this answer
 
Comments
venkata chaitanya 28-Aug-12 1:52am    
Thank you,i have done the same, but it worked fine for some time.After some hours, Session_Start event fires on each page request on my website.

Why would this happen?
nika2008 30-Aug-12 2:16am    
i am not sure how u open the txt file if u open it inside of Lock(); and UnLock();

i think is becose the reading the txt file is slow proccess and Application is much more faster then .txt reading and writte

try to do it outside of lock and unlock ; and dont update the txt file evry time i sugest u to do it evry 5 or 10 visitors.

sry if my english is bad

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