2

I am working on a Xamarin.Forms Android app, which needs to connect to a MySQL database.

I decided to use MySqlConnector, because it has async methods.

However, when I call OpenAsync() it throws System.IndexOutOfRangeException.

Open() works fine, but I would like to use async. Am I doing something wrong?

Here is my code:

private static MySqlConnection DBConnection;

internal static async Task<bool> SetAndTestConnectionString(string dbendpoint, int userid, string password)
{
    DBConnection = new MySqlConnection($" *connection string* ");
    bool connectresult= false;
    try
    {
        await DBConnection.OpenAsync();
        connectresult= true;
    }
    catch (Exception e)
    {
        connectresult= false;
        Console.WriteLine(e.Message);
    }
            
    return connectresult;
}
6
  • How are you calling SetAndTestConnectionString in turn? Commented Jan 12, 2022 at 17:44
  • @GSerg bool connectionsucceeded=await SetAndTestConnectionString(); Of course I supply the arguments. If the arguments I pass are incorrect, the exception it throws is somthing like "Cannot connect to host". So I don't think it has much to do with how I call it. Commented Jan 12, 2022 at 21:03
  • In that case, are you sure it's OpenAsync that throws this exception? Are you using the official MySql connector (that doesn't really support async) or something else? Commented Jan 13, 2022 at 7:18
  • @GSerg I'm using the "something else". And yes, I'm sure it is what throws the exception. See also this github issue Commented Jan 13, 2022 at 10:38
  • Well, yes, they receive a zero-sized memory span and try to access the first byte. Not much you can do about that. Commented Jan 13, 2022 at 11:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.