I was recently working on a login form in Visual Studio using C# with MySQL and I kept getting this error. I have been struggling on this for hours on end now with no luck. I have double checked pretty much everything and I don't think the database information is incorrect. It may be something with the user account not having sufficient permissions to do these actions, but I have checked that over and I think it has the right permissions on the database. Does anyone know how to fix this?
Error
MySql.Data.MySqlClient.MySqlException: 'Authentication to host 'localhost' for user 'username' using method 'sha256_password' failed with message: Access denied for user 'username'@'localhost' (using password: YES)'
"con" Variable
MySqlConnection con = new MySqlConnection("server = localhost; user=username;database=database;port=3306;password=pass");
int i;
Login Button Click Event
private void submit_Click(object sender, EventArgs e)
{
i = 0;
con.Open();
MySqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from login where username='"+username.Text+"' and password='"+password.Text+"'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
i = Convert.ToInt32(dt.Rows.Count.ToString());
if (i == 0)
{
MessageBox.Show("You have entered an invalid username or password.");
}
else
{
this.Hide();
Login fm = new Login();
fm.Show();
}
con.Close();
}