asp.net c# tutorials and examples code for web developers

Tuesday, October 21, 2008

How to get number of users online in asp.net c#

Get number of users online in asp.net
.Net framework Membership.GetNumberOfUsersOnline() method allow us to get the number of users currently accessing an application. Membership class GetNumberOfUsersOnline() method exists in System.Web.Security namespace.

GetNumberOfUsersOnline() method return value data type is System.Int32. this integer returned value represents the number of users currently accessing an ap.net application.

this method returned the number of users online for the current ApplicationName where the last activity date is greater than the current time less the UserIsOnlineTimeWindow.

Membership.ApplicationName property allow us to get or set the application name. Membership.UserIsOnlineTimeWindow property specify the number of minutes after the last activity date/time stamp for a user during which the user is considered online.

the following asp.net c# example code demonstrate us how can we get the number of currently online users for an asp.net application programmatically.
GetNumberOfUsersOnline.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e) {
        Label1.Text = "Now OnLine users: " +
        Membership.GetNumberOfUsersOnline().ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Membership class example: how to get number of users online in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>GetNumberOfUsersOnline method example</h2>
        <asp:LoginStatus ID="LoginStatus1" runat="server" />
        <br />
        <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="DarkGoldenrod"></asp:Label>
        <br /><br />
        <asp:LoginName ID="LoginName1" runat="server" FormatString="Hi {0}!" Font-Size="Large" ForeColor="Red" />

    </div>
    </form>
</body>
</html>






Related asp.net examples