ASP. NET allows you to record the number of historical visitors and the current number of online users.

Source: Internet
Author: User
* *********************************** In global. in asax, the following ************************
Start: Protected   Void Application_start (Object sender, eventargs E) // This will happen when the current application starts.
{
Sqlconnection con = New Sqlconnection ( " Server =.; database = countpeople; uid = sa; Pwd =; " );
Con. open ();
Sqlcommand cmd = New Sqlcommand ( " Select * From countpeople " , Con );
Int Count = Convert. toint32 (CMD. executescalar ());
Con. Close ();
Application [ " Total " ] = Count; // An application is a global variable and can be operated on by each session.
Application [ " Online " ] = 0 ;


}
 
Protected   Void Session_start (Object sender, eventargs E) // Once the client is connected to the server, this event will occur.
{
Session. Timeout = 1 ;
Application. Lock (); // After the session is locked, only the session can be session
Application [ " Total " ] = ( Int ) Application [ " Total " ] + 1 ;
Application [ " Online " ] = ( Int ) Application [ " Online " ] + 1 ;
Application. Unlock (); // Unlock after session completion

}

End: Protected   Void Session_end (Object sender, eventargs E)
{
Application. Lock ();
Application [ " Online " ] = ( Int ) Application [ " Online " ] - 1 ;
Application. Unlock ();

}

Protected   Void Application_end (Object sender, eventargs E)
{
Sqlconnection con = New Sqlconnection ( " Server =.; database = countpeople; uid = sa; Pwd =; " );
Con. open ();
Sqlcommand cmd = New Sqlcommand ( " Updata countpeople set num = " + Application [ " Total " ], Con );
Cmd. executenonquery ();
Con. Close ();

}

**************************************** * In the aspx file, the following ********************************* Private   Void Page_load ( Object Sender, system. eventargs E)
{
This . Lbltotal. Text = Application [ " Total " ]. Tostring ();
This . Lblonline. Text = Application [ " Online " ]. Tostring ();
// Place user code here to initialize the page
}

Ninetynine original

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.