I want to update a user name and password stored in a SQLite database with a new username and password provided by a user so that they can update their user details.
I have already tried to look up solutions to my problem and have found similar users with a similar requirement but when I try to implement their solutions with my code it doesn't seem to work.
SQLiteConnection con = new SQLiteConnection("Data Source=Users.sqlite;Version=3;");
SQLiteCommand cmd = new SQLiteCommand("select * from UserInfo where username like @username and password = @password;", con);
cmd.Parameters.AddWithValue("@username", oldusername);
cmd.Parameters.AddWithValue("@password", oldpassword);
con.Open();
SQLiteDataReader sdr = cmd.ExecuteReader();
if ((sdr.Read() == true))
{
MessageBox.Show("Username and Password Updated Successfully!",
"Task Completed");
string update ="UPDATE UserInfo SET UserName='" + newusername + "', Password='" + newpassword + "' WHERE (Username='" + oldusername + "' AND Password ='" + oldusername + "');";
con.Close();
}
else
{
MessageBox.Show("Invalid username or password",
"Incorrect details entered");
}
The problem is that my code checks if the old username and password is stored in the UserInfo
table but it doesn't update the table with the new username and password. I don't know what I am doing wrong so it would be great if someone could correct me and maybe improve the code. Thanks in advance.