vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, June 22, 2011

How to find number of online user/visitors in asp.net


Hi.
In this article i am going to describe about to find the number of online user in website. This type of process we have to write the code in
Global.asax file .In previously i wrote an article about to find number of views or count in page .For your reference i mention it here...


Global.asax file you can find in Solution explorer. Click Solution explorer->ADD New Item->Choose Global.asax file
In this file you Can find the following Structure:
Application start, Application End, session start ,session end Function ,So write the below code in the file

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        Application("user") = 0
    End Sub
   
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub
       
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
    End Sub

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
        Application.Lock()  ' Its used to open only one time  for session,suppose the user do referesh page  it will not count the counter.
        Application("user") = Application("user") + 1
        Application.UnLock()
               
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a session ends.
        ' Note: The Session_End event is raised only when the sessionstate mode
        ' is set to InProc in the Web.config file. If session mode is set to StateServer
        ' or SQLServer, the event is not raised.
        Application.Lock() ' Its used to open only one time  for session,suppose the user do referesh page  it will not count the counter.
        Application("user") = Application("user") - 1
        Application.UnLock()
       
    End Sub
      
</script>

Also Find web.config file in that file write the below code in session.
Session is used when user went offline then its decrease one count from the application ("user").
                <system.web>
                                <sessionState mode="InProc" timeout="5"/>
                             
                                <compilation debug="true">

Then In your Homepage i.e default.aspx file keep one label and the enter the following code in serverside
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          Label1.Text = Application("user")
    End Sub

Now process is over,  you can open your page check the page in different browser it will count the online user..
i hope this one helpful to you ..

0 comments:

Post a Comment