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

Jignesh KumarRank #91 · 22.9K reputationPosted May 3, 2024, 12:09 AM EDT
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 NULLprivate 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; }Jayraj ChhayaRank #304 · 8.2K reputationPosted May 3, 2024, 1:54 AM EDT
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 sessionThis 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.
Jignesh KumarRank #91 · 22.9K reputationPosted May 5, 2024, 2:25 AM EDT
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(); }Sushant TorankarRank #3,026 · 476 reputationPosted May 3, 2024, 11:32 AM EDT
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?