Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhelpful exception message when reading NULL value #1092

Closed
bgrainger opened this issue Nov 28, 2021 · 1 comment
Closed

Unhelpful exception message when reading NULL value #1092

bgrainger opened this issue Nov 28, 2021 · 1 comment

Comments

@bgrainger
Copy link
Member

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.

@bgrainger
Copy link
Member Author

Improved in 2.1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant