‘Unable to connect to any of the specified mysql hosts.’ error

Issue

I have a Xamarin app where you connect to a database. There are no errors in the error list but when I run the code, I get this error: Unable to connect to any of the specified MySQL hosts. I use Xamarin.MySql.Data.

I use the same connection string in a windows forms app and it works totally fine. I wrote it yesterday, it is not an old build.

Here is my code:

MySqlConnection conn  new MySqlConnection("ServermyServerAddress;DatabasemyDataBase;UidmyUsername;PwdmyPassword;");
await conn.OpenAsync();

MySqlCommand cmd  new MySqlCommand("SELECT * FROM users WHERE USERNAME  '" + username.Text + "' AND PASSWORD  '" + password.Text + "'", conn);

cmd.Connection  conn;
MySqlDataReader rdr  cmd.ExecuteReader();

if(rdr.HasRows)
{
    Toast.MakeText(this, "Successfully Logged In", ToastLength.Short).Show();
}
else
{
    Toast.MakeText(this, "Invalid Username/Password", ToastLength.Short).Show();
 }

Solution

I solved the issue by replacing Xamarin.MySql.Data with MySqlConnector. It seems Xamarin.Mysql.Data is outdated or something.

Answered By – Alfe

Leave a Comment