Why am I getting an error when connecting to MySql in C# .Net Core 3

I have just updated from MySql.Data.MySqlClient .Net Core Class Library version 8.0.21 to 8.0.22 and it seems to have caused errors for some of my SQL queries. I am not getting the following error:

“Non-negative number required. (Parameter ‘count’)”

When I am trying to run the following code:

DataTable dt = new DataTable();
try {
    using (MySqlConnection con = new MySqlConnection(ConnectionString)) {
        using (MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM shortcuts WHERE user = @id", con)) {
            da.SelectCommand.Parameters.AddWithValue("@id", userId);
            da.Fill(dt);
        }
    }
} catch (Exception ex) {
    Console.WriteLine(ex.Message);
    dt = null;
}

I cannot see what the error is complaining about and I only have 8 records in the “shortcuts” table that I am trying to retrieve data from. Since I haven’t changed any of my code and it was working before I updated my NuGet package, I am assuming that it is an issue with the latest build. I did, however, try to downgrade back to the version I was on and there was no luck.

I don’t know if this is the right place to report errors or get information but I would appreciate any help. Here are some things that I have tried:

  • Downgrading back to version 8.0.21
  • Restarting the MySql service (I am using XAMPP and my local machine at the moment)
  • Adding “da.SelectCommand.Parameters.Clear();” at the top of the code to make sure there are no parameters stuck from previous queries, as it will work for other queries but not this one.

Thanks for any assistance you can provide

Answer

Are you querying a table that has a CHAR(36) column (i.e., a GUID) containing NULLs? If so, you’re encountering bug 101252, a regression introduced in MySql.Data 8.0.22 (that won’t be fixed until 8.0.24, probably in late April 2021).

You could roll back to 8.0.21, but I would personally recommend switching to MySqlConnector (disclaimer: I’m the lead contributor) as it has better compatibility with .NET Core and fixes many bugs present in MySql.Data.