I initially learned a bit of coding with website, and then I later changed to learn web application. I hosted my website to a live server to test and know how far I have learned, and on the website I had a feature that showed the last login time and date of a user when a user login (the time and date were set to Local Time); it was working perfectly. Then when I changed to web application, using the same code that display the date and time from database, the DateTime to local does not work anymore, I now have a minus seven hours (-7hrs) difference. I really do not know why this happened.
This is how it works:
When a user login, an update is performed in the “LoginLast” column of the database table, and the previous value in the “LoginLast” column is sent as a session onto the next page.
protected void ValidateUser(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Username.Text) & !string.IsNullOrEmpty(passwordtxt.Text))
{
string connectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Id, IdRoles FROM Users WHERE email = @email AND password = @password", con))
{
con.Open();
cmd.Parameters.AddWithValue("@email", Username.Text.Trim());
cmd.Parameters.AddWithValue("@password", passwordtxt.Text.Trim());
//string Id = Convert.ToString(cmd.ExecuteScalar());
SqlDataReader sdr = cmd.ExecuteReader();
string UserId = string.Empty, RoleId = string.Empty;
if (sdr.Read())
{
UserId = Convert.ToString(sdr["Id"]);
RoleId = Convert.ToString(sdr["IdRoles"]);
}
con.Close();
if (string.IsNullOrEmpty(UserId))
{
int user = 0;
using (SqlCommand cmd2 = new SqlCommand("SELECT Uid FROM Users WHERE password = @password COLLATE SQL_Latin1_General_CP1_CS_AS AND email = @email AND password = @password"))
{
cmd2.CommandType = CommandType.Text;
cmd2.Parameters.AddWithValue("@email", Username.Text.Trim());
cmd2.Parameters.AddWithValue("@password", passwordtxt.Text.Trim());
cmd2.Connection = con;
con.Open();
user = Convert.ToInt32(cmd2.ExecuteScalar());
con.Close();
}
if (user > 0)
{
Session["user"] = UserId;
con.Open();
string check = "SELECT LoginLast, IsActive from Users WHERE Id = @Id";
using (SqlCommand cmd6 = new SqlCommand(check, con))
{
cmd6.Parameters.AddWithValue("@Id", Session["user"]);
Session["LoginLast"] = Convert.ToDateTime(cmd6.ExecuteScalar());
}
string UpdateLog = @"UPDATE Users SET LoginLast=@dateandtime, IsActive=@IsActive WHERE Id = @Id";
using (SqlCommand cmd4 = new SqlCommand(UpdateLog, con))
{
cmd4.Parameters.AddWithValue("@dateandtime", DateTime.UtcNow);
cmd4.Parameters.AddWithValue("@IsActive", "1");
cmd4.Parameters.AddWithValue("@Id", Session["user"]);
cmd4.ExecuteNonQuery();
con.Close();
}
SqlCommand cmd5 = new SqlCommand("SELECT RoleName From [Roles] WHERE IdRoles = @IdRoles", con);
con.Open();
cmd5.Parameters.AddWithValue("@IdRoles", RoleId);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd5);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
string roles = dt.Rows[0]["RoleName"].ToString().Trim().ToLower();
if (roles == "admin")
{
Session["user"] = UserId;
FormsAuthentication.RedirectFromLoginPage(UserId, true);
Response.Redirect("~/administration/AdminDashboard.aspx");
}
else if (roles == "adminuser")
{
Session["user"] = UserId;
FormsAuthentication.RedirectFromLoginPage(UserId, true);
Response.Redirect("dashboard.aspx");
}
else if (roles == "user")
{
Session["user"] = UserId;
FormsAuthentication.RedirectFromLoginPage(UserId, true);
Response.Redirect("dashboard.aspx");
}
else
{
Response.Redirect("Login.aspx");
}
}
}
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "Invalid Login Details";
}
}
}
}
else
{
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Text = "All Fields are Required";
}
}
Then on the page load event of the next page, here is how I show the last login date and time
LoginDateTime.Text = Convert.ToDateTime(Session["LoginLast"]).ToLocalTime().ToString("dddd, MMMM d, yyyy h:mm tt") + ".";
I really don't why this has happened, but I seek for help with learning how to resolve this.
Thank you