1

Edit: Just to be clear, I'm not asking about (Null vs DBNull.Value) or (IsDBNull vs IsNull).


Well, I always use IsDBNull() to evaluate database fields and check if the value is DBNull, but I noticed that in many code examples, people use DBNull.Value for the same purpose. Here's how:

If IsDBNull(obj) Then DoSomething()      ' ---> My approach
If obj = DBNull.Value Then DoSomething() ' ---> Theirs.

Also in C#:

if (obj == DBNull.Value) {DoSomething();}

I did some search to find out if they're the same or if there's any difference between them, but can't find any useful articles that show these two particular evaluation approaches together.

This MSDN link demonstrates how DBNull.Value.Equals does the same thing as IsDBNull function, but never mentions/suggests using DBNull.Value for evaluation, without the use of Equals function.

So, I'm really wondering, is there any actual difference between these two so that one of them is recommended over the other? Or do they do the exact same thing?

  • 1
    That second line should use `Is` rather than `=` but, apart from that, they are functionally equivalent. That `IsDBNull` method probably uses that second method internally. – jmcilhinney Feb 16 '17 at 00:32
  • [Here is the .Net implementation.](https://github.com/Microsoft/referencesource/blob/master/System.Data/System/Data/SqlClient/SqlDataReader.cs#L2917) – Will Ray Feb 16 '17 at 00:44
  • @WillRay, that is the implementation of the `SqlDataReader.IsDBNull` method, which is not the one specified in the question. The method in the code above is `Microsoft.VisualBasic.Information.IsDBNull`. – jmcilhinney Feb 16 '17 at 02:18
  • one reason why I don't use vb.net functions is because we will eventually have all code in c#. And if you use `Trim(str)` it is not translatable by converters. But if you use `str.Trim()` it will convert to c#. To answer your question, all you need to do is to download free disassembler and look at the function code. But you really shouldn't worry what `IsDbNull` does because you should use `DBNull.Value.Equals` – T.S. Feb 16 '17 at 03:42
  • Possible duplicate of [What is the difference between null and System.DBNull.Value?](http://stackoverflow.com/questions/4958379/what-is-the-difference-between-null-and-system-dbnull-value) – Chetan Sanghani Feb 16 '17 at 04:29
  • @jmcilhinney, how about the C# version? I've seen this many many times (Like [here](http://stackoverflow.com/a/12508016/4934172) for example). – 41686d6564 stands w. Palestine Feb 16 '17 at 07:55
  • @T.S. I already mentioned in my question that both `IsDBNull` and `DBNull.Value.Equals` do the same thing. Also some VB functions really come in handy and are more readable. Microsoft says in the MSDN link above: "*However, this method is rarely used because there are a number of other ways to evaluate a database field for missing data. These include the Visual Basic IsDBNull function...*". Side note: I don't use `Trim(str)` unless it's VB6 :D – 41686d6564 stands w. Palestine Feb 16 '17 at 08:03
  • 1
    @ChetanSanghani You don't even need to open the other question. Just by looking at their both titles, They're obviously talking about two different things :) – 41686d6564 stands w. Palestine Feb 16 '17 at 08:07
  • 1
    `if (obj == DBNull.Value) {DoSomething();}` in C# is exactly equivalent to `If obj Is DBNull.Value Then DoSomething()` in VB. – jmcilhinney Feb 16 '17 at 08:19
  • @jmcilhinney [apparently you're right](http://stackoverflow.com/a/814880/4934172). I didn't know that, and I guess that's what made the confusion. Thank you :) – 41686d6564 stands w. Palestine Feb 16 '17 at 08:27

1 Answers1

-1

No, they are not the same. DBNull.Value is effectively a constant, while IsDBNull is a function that returns true or false.

The IsDbNull is a test flag returning Boolean and is a shortcut expression for use in If Statements. You can STILL use the Obj = DBNull.value method but IsDbNull is the preferred method according the MS.

However, if you want to use a select case statement, you still need to use the DBNull.value object

Select case Obj
    Case DBNull.value
    Case "Whatever"
End Select

Of course, you ALSO need DBNull.Value if you want to set a field back to a null value.

It is similar to Double.IsNaN and Double.NaN.

Trevor_G
  • 1,331
  • 1
  • 8
  • 17
  • That's not what he is concerned. He is asking whether the result of comparing obj isDBNull() is the same as obj == DBNull.value !! The answer is YES, they are the same. – Jenna Leaf Jun 13 '18 at 18:30