4
Answers

Active users on portal

hello,

i am working in asp.net and oracle as database.

I have approximately 1000 agents in my users db, I want to know how many total agents are currently logged in portal. I want to show count of those agents like : Out of 1000 Agents 150 agents are logged in

I have log table which stores agent logged in time and logout time.

Anyway to get this output ?

Thanks in advance

Answers (4)

3
Photo of Jignesh Kumar
30 39.6k 2.9m 1y

Hello Sushant,

You can achieve this using global.ascx file,

// Global.asax.cs

using System;
using System.Web;

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        // Initialize active users count
        Application["ActiveUsers"] = 0;
    }

    void Session_Start(object sender, EventArgs e)
    {
        // Increment active users count
        Application.Lock();
        Application["ActiveUsers"] = (int)Application["ActiveUsers"] + 1;
        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e)
    {
        // Decrement active users count
        Application.Lock();
        Application["ActiveUsers"] = (int)Application["ActiveUsers"] - 1;
        Application.UnLock();
    }
}

If you wish to display count on your webpage then you can display the active users count in a webpage:

// Inside your .aspx.cs file

protected void Page_Load(object sender, EventArgs e)
{
    int activeUsers = (int)Application["ActiveUsers"];
    // Display active users count on the webpage
    lblActiveUsers.Text = "Active Users: " + activeUsers.ToString();
}
2
Photo of Jayraj Chhaya
305 6k 98.4k 1y

To determine the count of agents currently logged in, you can utilize SQL queries in Oracle. By querying the log table for active sessions (where logout time is null or within a specific range), you can calculate the number of agents logged in. Here's a sample query to achieve this:

SELECT COUNT(*) AS LoggedInAgents
FROM log_table
WHERE logout_time IS NULL; -- Assuming NULL logout_time indicates an active session

This query counts the number of agents with no logout time recorded, indicating they are currently logged in. You can then display this count in your portal interface.

2
Photo of Jignesh Kumar
30 39.6k 2.9m 1y

Hello Sushant,

You can create one method using which you can get active use count,

As per my understanding once the user logs in you will make an entry in the table, so using below sql query you can find logs in user count,

SELECT COUNT(UserId) AS active_users_count FROM log_table WHERE logout_time IS NULL
private string connectionString = "your_connection_string_here";

    public int GetActiveUsersCount()
    {
        int activeUsersCount = 0;

        string query = "SELECT COUNT(UserId) AS active_users_count FROM log_table WHERE logout_time IS NULL";

        using (OracleConnection connection = new OracleConnection(connectionString))
        {
            using (OracleCommand command = new OracleCommand(query, connection))
            {
                try
                {
                    connection.Open();
                    activeUsersCount = Convert.ToInt32(command.ExecuteScalar());
                }
                catch (Exception ex)
                {
                    // Handle exceptions
                    Console.WriteLine("Error: " + ex.Message);
                }
            }
        }

        return activeUsersCount;
    }
1
Photo of Sushant Torankar
1.3k 306 38.7k 1y

Thank you all for reply.

I was thinking about below code, but i think it will take more time to load as there are many users.

Any fast way to achieve, maybe from global.asax file or ApplicationSession?