Which of IsDBNull and IsNull should be used?

24,309

Solution 1

Short answer: use the first way, it is faster, because the first method uses a pre-computed result, while the second method needs to recompute it on the fly every time you call it.

Long answer: (you need to read C# code to understand this part; MS supplies framework code in C#, but VB programmers should be able to get the general idea of what's going on)

Here is what happens inside the IsNull call of DataRow:

public bool IsNull(string columnName) {
    DataColumn column = GetDataColumn(columnName);
    int record = GetDefaultRecord();
    return column.IsNull(record);
}

The column.IsNull performs a quick assertion, and forwards the call to DataStorage, an internal class:

internal bool IsNull(int record) {
    Debug.Assert(null != _storage, "no storage");
    return _storage.IsNull(record);
}

Finally, here is what _storage.IsNull does:

public virtual bool IsNull(int recordNo) {
    return this.dbNullBits.Get(recordNo);
}

Since dbNullBits is a BitArray, this operation completes very quickly.

Now consider what the indexer myDataRow("Column1") does (you call this indexer before passing its result to IsDBNull):

get {
    DataColumn column = GetDataColumn(columnName);
    int record = GetDefaultRecord();
    _table.recordManager.VerifyRecord(record, this);
    VerifyValueFromStorage(column, DataRowVersion.Default, column[record]);
    return column[record];
}

Note that the first two lines of IsNull method and the indexer are identical. However, the three rows that follow need to perform validation, and fetch the value itself. Only after that your code can start computing its target value - a flag that tells it if the value is DBNull or not. This requires more computation, but more importantly, it requires some computation every time you perform the check. This is slower than using a pre-computed value.

Solution 2

.NET almost never gives you two ways to do the same thing by accident. DataRow.IsNull() is more efficient, it avoids having to retrieve the column value and then checking for IsDBNull. Internally it already keeps track of whether a column is null, determined when the row was created. So IsNull() can give you that very quickly. It should therefore be your preference.

Solution 3

I did bit of findings and found interesting facts which provides more insight on usage of DataRow.IsNull OR IsDBNull.

DataRow.IsNull - Gets a value that indicates whether the specified DataColumn contains a null value. Convert.IsDBNull - Returns an indication whether the specified object is of type DBNull.

References: DataRow.IsNull and IsDBNull

The most interesting discussion which provides clear conclusion can be drawn from Performance Consideration

Nearly similar discussion were done earlier, here are references:

Finding null value in dataset datarow isnull...

Most efficient way to check for dbnull...

Avoid checking for datarow isdbnull...

Solution 4

From a database design and usage point of view, IsNull is the correct and accepted way to interrogate the value of a column, based on use of various database technologies including SQL, DB2, OLAP, MOLAP, RDBMS, MDBMS, SPSS, Essbase, etc. By definition Null is the absence of a value, a unknown, and Null is not even equal to Null, so any adjunct to Null is just a matter of convenience.

Solution 5

When dealing with DataRow data, it's ideal to use the IsDBNull() function. IsDBNull() has the advantage that it checks if your object represents a null, rather than simply being null itself, and that's an important difference. When you are interrogating a data row item which is null in the database, the item itself exists as an object, but it represents a NULL value. If you use IsNull() you will miss NULL values.

Share:
24,309

Related videos on Youtube

CJ7
Author by

CJ7

Updated on July 05, 2022

Comments

  • CJ7
    CJ7 almost 2 years

    If in VB.NET I have DataRow and I want to test whether a column value is Null, should I use:

    myDataRow.IsNull("Column1")
    

    OR

    IsDBNull(myDataRow("Column1"))
    
    • Matt Wilko
      Matt Wilko almost 11 years
      I think the two examples you gave are functionally identical
  • CJ7
    CJ7 almost 11 years
    Why is null returned for SELECT statements? Is DBNull returned for all other SQL statements?
  • Mike Perrenoud
    Mike Perrenoud almost 11 years
    @CJ7, it's marshaled to a null when it's put into a property or a data row.