There are many many (mostly) minor mistakes in your code:
- use some kind of Id fields in your sql tables
- never do an update like you did (update the field WHERE this field is equals to...)
- create your own class and bind the query result to this class
- when a class implements IDisposable interface, always use the keyword 'using'
- never ever user string concatenation in sql queries!!! SQL INJECTION!!! always use parametrized sql queries
Here's a simple example for your form. Let's suppose your
user.register table has the following columns:
- Id
- Username
- Password
Now let's create your own class (maybe right under your button click
event, so it can be private this time):
private class MyUser
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
Then your button click event should look like this:
private void Btnchange_Click(object sender, EventArgs e) {
if (!textnewpassword.Text.Trim().Equals(textconfirmpassword.Text.Trim()))
{
throw new ArgumentException("New password and confirm password should be same!");
}
List<MyUser> myUsers = new List<MyUser>();
using (MySqlConnection con =
new MySqlConnection(
"server=localhost;user id=root;persistsecurityinfo=True;database=user;password=1234"))
{
using (MySqlCommand cmd = new MySqlCommand("select * from user.register where Username=@user and Password=@pass", con))
{
cmd.Parameters.AddWithValue("@user", textusername.Text.Trim());
cmd.Parameters.AddWithValue("@pass", textoldpassword.Text.Trim());
if (cmd.Connection.State != ConnectionState.Open) cmd.Connection.Open();
using (MySqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
myUsers.Add(new MyUser
{
Id = (int)dr["Id"],
Username = dr["Username"].ToString(),
Password = dr["Password"].ToString()
});
}
}
if (cmd.Connection.State == ConnectionState.Open) cmd.Connection.Close();
}
if (!myUsers.Any())
{
throw new ArgumentException("No users found with the given username/password pair!");
}
if (myUsers.Count != 1)
{
throw new ArgumentException("More than 1 user has the same username and password in the database!");
}
MyUser user = myUsers.First();
user.Password = textnewpassword.Text.Trim();
using (MySqlCommand cmd = new MySqlCommand("update user.register set Password=@pass where Id=@id"))
{
cmd.Parameters.AddWithValue("@pass", user.Password);
cmd.Parameters.AddWithValue("@id", user.Id);
if (cmd.Connection.State != ConnectionState.Open) cmd.Connection.Open();
cmd.ExecuteNonQuery();
if (cmd.Connection.State == ConnectionState.Open) cmd.Connection.Close();
}
} }
...and so on.
ifstatement, does it stop there when run? See this for sql injection prevention. See this for handing passwords more securely.useridin yourWHEREclause.Bobby Tablesto understand what is wrong with this code. Best case, someone enters';--to delete all passwords. Worst case, someone writes'; drop table user.register;--. Don't use dynamic SQL, use parameterized queries. Don't write your own user management code either. The proper way to store passwords is to salt them, hash them at least 1000 times with a strong cryptographic algorithm and only store the hash. Frameworks like ASP.NET Core Identity already do this correctly