List Users Online "Active Users"

Posted on 2005-05-06
Last Modified: 2010-04-07
I have a web application ASP.NET
I need to know how can I list the users online? I hope that you can direct me into a solution for this question.

I have a login page login.aspx and this is how I validate the user

        Dim strConnStr As String

        Dim Cnn As New OleDb.OleDbConnection("Provider=SQLOLEDB.1;data .      etc......")
        Dim Mycmd As New OleDb.OleDbCommand("select * from users_tbl", Cnn)

        Dr = Mycmd.ExecuteReader()
        While Dr.Read
            If Trim(UCase(Dr("Usercode"))) = Me.txtUserID.Text.ToUpper Then
                If Trim(UCase(Dr("UserPass"))) = Me.txtPassword.Text.ToUpper Then
                    'Now we need to load the sesstion Variables

                    Session("UserID") = Trim(Dr("UserCode")).ToUpper


                End If

            End If
        End While
Is there is a way that I can display a list of all users logged in to the site. If you can provide a sample code it will be greate or direct me to the proper direction.

NOTE: I'm using a SQL server that has users_tbl to validate the user login and I also post the time/date of their login.

Question by:wirelessgiant
    LVL 23

    Expert Comment

    you could use add a property in the Global.asax.vb/Global.asax.cs which returns a DataTable that holds the users' information. add that as a user logs on and remove that as the user logs off
    LVL 23

    Expert Comment

    Imports System.Data

    Public Class Global
          Inherits System.Web.HttpApplication
          'table schema
          Private _table As DataTable
          Public ReadOnly Property Users() As DataTable
                      If _table Is Nothing Then
                            _table = New DataTable("Users")
                            With _table.Columns
                                  .Item(0).AllowDBNull = False
                                  .Item(1).AllowDBNull = False
                                  .Item(2).AllowDBNull = False
                                  _table.PrimaryKey = New DataColumn() { .Item(0) }
                            End With
                      End If
                      Return _table
                End Get
          End Property
          'standard stuff
    End Class
    LVL 23

    Accepted Solution

    change to
     Private Shared _table As DataTable
     Public Shared ReadOnly Property Users() As DataTable

    so now you can add to the data table or remove the entries after log on and log off like so:

    Dim userRow As DataRow = Global.Users.Rows.Find(<id>)
    If userRow Is Nothing Then
      userRow = Global.Users.NewRow()
      userRow("UserID") = <id>
    End If

    userRow("Logon") = Now
    userRow("Active") = True

    If userRow.RowState = DataRowState.Detached Then
    End If


    when logoff,

    Dim userRow As DataRow = Global.Users.Rows.Find(<id>)
    If Not userRow Is Nothing Then
    End If

    LVL 23

    Expert Comment

    you could also have a periodic check on the DataTable to flush out any old entries... based on DateTime

    Author Comment


    Thank you b1xml2 for a well detail solution.  I have one more question is (I'm not an expert in this)
    what is the best way to get the output from Global.Users. .. to get a list of these users and add them to a list box.

    I really thank you for your response. :)
    LVL 23

    Expert Comment

    to get them to a listbox,

    listBox.DataValueField = "UserId"
    listBox.DataTextField = "UserName"
    listBox.DataSource = Global.Users.DefaultView

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now