I need to capture the logout time and login time. I can capture the login time already but I cannot capture the logout time. I would appreciate all the help that you had given me. Thank you.
This is the coding in the logout button:
protected void logoutButton_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("SELECT TOP 1 LogId FROM LoggedInUsers where MemberID = @MemberID ORDER BY LogId DESC");
cmd.Parameters.AddWithValue("@MemberID", Session["MemberID"]);
Session.Abandon();
}
I also need to do in global.asax. And this is the coding:
protected void Session_End(object sender, EventArgs e)
{
if (Session["MemberId"] != null)
{
UpdateLoggedInUsersLogoutTime(Convert.ToInt32(Session["MemberId"].ToString()));
}
//string emailaddress = (string)Session["emailaddress"];
}protected Boolean UpdateLoggedInUsersLogoutTime(int MemberID)
{
SqlConnection connection = null;
SqlCommand command = null;
Boolean successFlag = false;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
connection = new SqlConnection(connectionString);
connection.Open();
//retrieve the logid
string connectionString1 = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
connection = new SqlConnection(connectionString1);
connection.Open();
//prepare sql statements
//string sql = "SELECT * from LoggedInUsers where emailaddress='" + emailaddress + "' And Password='" + password + "'";
//Response.Write(sql);
//command = new SqlCommand(sql, connection);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
/* add value to Session */
Session["LogId"] = dt.Rows[0]["LogId"].ToString();
SqlCommand cmd = new SqlCommand("SELECT TOP 1 LogId FROM LoggedInUsers where MemberID = @MemberID ORDER BY LogId DESC");
cmd.Parameters.AddWithValue("@MemberID", MemberID);
//pass the session
int logid = (int)Session["MemberId"];
DateTime dateTime = DateTime.Now;
string s = dateTime.ToString();
//update record based on logid
SqlCommand cmd1 = new SqlCommand("UPDATE LoggedInUsers set LogoutTime = '" + s + "' Where MemberID = @MemberID");
//prepare sql statements
string sql1 = "UPDATE LoggedInUsers set LogoutTime= '" + s + "' where emailaddress=@EmailAddress";
//Response.Write(sql + "<br />");
//command = new SqlCommand(cmd1, connection);
//command.Parameters.Clear();
command.Parameters.AddWithValue("@LogoutTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
command.Parameters.AddWithValue("MemberID", MemberID);
int rowCount = command.ExecuteNonQuery();
//if (rowCount != 0)
if (rowCount == 1)
{
successFlag = true;
//Response.Write("Operation success.<br/>");
}
else
{
//Response.Write("Operation failed<br/>");
}
}
}
catch (Exception ex)
{
//Response.Write("Error:" + ex.Message);
}
//cleanup object
finally
{
if (connection != null)
connection.Close();
}
return successFlag;
}
}
}