Show Online Username
Now I am going to discuss, how to show who is online on my site - showing the Online Username , We know already how to show the Number of users online using Application Variables on Global.asax file.
But here i am going to show the online usersName.So, In my case i used Login Control and implement SecurityMembershipProvider and Roleprovider.Whenever the user Login - the LastAcivityDate field in the Users table will be updated with Current DateTime. The code has written on the SecurityMembership Provider.
When the user navigating the page ie: OnPostback the page - LastActivityDate will be Updated automatically, this will make sure the userName was active on the site.
if i say Simply it will show the online Username for the last 15 Minutes.
Code-behind:
// here getting the 15 Minutes, if you want the time difference you use it here..ie: to // update the your page at regular intervals. TimeSpan onlineSpan = new TimeSpan(0,15,0); // here i minus the 15 Minutes from the Current Datetime and pass it as a parameter DateTime compareTime = DateTime.Now.Subtract(onlineSpan); using (SqlCommand cmd = new SqlCommand("SELECT users.username from users where users.LastActivityDate > @CompareDate ", con)) { cmd.Parameters.Add("@CompareDate", SqlDbType.DateTime).Value = compareTime; using(SqlDataAdapter adap = new SqlDataAdapter()) { adap.SelectCommand = cmd; } } DataTable dt = new DataTable(); adap.Fill(dt); OnlineuserList_GridView.DataSource = dt; OnlineuserList_GridView.DataBind();
Read more »