Closed
Description
using var cmd = new MySqlCommand("select null;", connection);
using var reader = cmd.ExecuteReader();
reader.Read();
reader.GetInt32(0); // InvalidCastException: Specified cast is not valid.
reader.GetInt64(0); // InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.Int64'.
// MySql.Data
reader.GetInt32(0); // SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
User at https://stackoverflow.com/q/70132978/23633 was probably not helped by the low-information exception message. Commenters would also probably have been quicker to help him if the GetInt32
message was more like the GetInt64
message.
MySql.Data has a different exception type, which is even more clear; MySqlConnector uses InvalidCastException
by design, so it's not necessary to change the exception type but it could have a better message.