private void Page_Load(object sender, System.EventArgs e)
{
if (Application["online"] == null)
{
DataTable dtnew = new DataTable();
dtnew.Columns.Add("userid",typeof(string));
dtnew.Columns.Add("lastaccesstime",typeof(DateTime));
Application["online"] = dtnew;
}
DataTable dt = Application["online"] as DataTable;
DataRow[] rows = dt.Select("userid='"+Session.SessionID+"'");
if (rows.Length == 1)
{
DataRow dr = rows[0];
dr["lastaccesstime"] = DateTime.Now;
}
else
{
DataRow dr = dt.NewRow();
dr["userid"] = Session.SessionID;
dr["lastaccesstime"] = DateTime.Now;
dt.Rows.Add(dr);
}
for(int i = 0 ; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
TimeSpan t = DateTime.Now - Convert.ToDateTime(dr["lastaccesstime"]);
if (t.Seconds > 8)
{
dr.Delete();
}
}
dt.AcceptChanges();
this.Label1.Text = dt.Rows.Count.ToString();
}