Skip to content

Unhelpful exception message when reading NULL value #1092

@bgrainger

Description

@bgrainger
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions