System.Data.SQLite

View Ticket
Login
Ticket Hash: 070b05bef9352a8657ce9c1e49fad0c935848cd8
Title: SQLiteDataReader.IsDBNull(-1) should throw an exception
Status: Open Type: Feature_Request
Severity: Important Priority:
Subsystem: Resolution:
Last Modified: 2021-01-12 11:50:41
4.46 years ago
Created: 2021-01-12 11:50:41
4.46 years ago
Version Found In: 1.0.112
User Comments:
anonymous added on 2021-01-12 11:50:41:
Calling SQLiteDataReader.IsDBNull with an invalid index (such as -1 or index >= reader.FieldCount) returns true instead of throwing an exception (ADO.NET throws an IndexOutOfRangeException e.g.).

Let's assume we have a piece of code such as:
//Method:
int SafeToInt(DbDataReader reader, string colName) {
    if (reader.IsDBNull(reader.GetOrdinal(colName))
        return someDefaultValue;
    else
        return reader.GetInt32(reader.GetOrdinal(colName));
}

//Caller:
int myValue = SafeToInt(reader, "ColumnNameWithTypo");    //Oops, the programmer made a typo with a column name..


Since GetOrdinal returns -1 if it couldn't find a column name (ADO.NET throws an exception in this case as well) this is directly passed to IsDBNull which evaluates to true and the default value is used. It's not obvious to the developer that he accidentially mistyped the column name. This is an annoying bug in the program and potentially hard to track down. So I think an exception is more appropriate when calling IsDBNull with an invalid index.