I don't understand why this code doesn't update the data table:
SqlCommand cmd = new SqlCommand("Select Count(*) From Utilizatori where NumeUtilizator='"+ usermailtxt +"' and Parola='"+ passtxt +"'", conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
DateTime dateTime = DateTime.Now;
SqlCommand cmmd = new SqlCommand("Update Utilizatori Set UltimaUtilizare = '"+ dateTime + "' where IdUtilizatori='"+sdr[0]+"' ", conn);
cmmd.ExecuteNonQuery();
}
Here I tried making something like "last seen on dd/MM/yyyy at hh:mm", updating a data table after successfully logging in (the email is useless):
Idutilizatori | NumeUtilicator | Parola | EmailUtilizator | UltimaUtilizare |
---|---|---|---|---|
1 | bruh | admin | [email protected] | 4/11/2024 12:40 |
2 | admin | admin | asd | 4/11/2024 1:12 |
3 | NULL | NULL | NULL | 4/11/2024 1:11 |
Basically, I want to search by UserId
, and it didn't update anything. Even when I changed it to user name (NumeUtilizator
), the result was the same.
SqlCommand cmmd = new SqlCommand("Update Utilizatori Set UltimaUtilizare = '"+ dateTime + "' where NumeUtilizator=@usertxt ", conn);
Not even when I tried using parameters:
SqlCommand cmmd = new SqlCommand("Update Utilizatori Set UltimaUtilizare = '"+ dateTime + "' where NumeUtilizator=@usertxt ", conn);
cmmd.Parameters.AddWithValue("@usertxt", usermailtxt.Text);
cmmd.ExecuteNonQuery();
So, how do I update by ID?
I think you can't use the same connection for updating, while the reader is open. You could try using a second connection for the update or close the reader before executing the update.
This may be a duplicate to C# While reading data with DataReader cannot update same record