8

I have the following line of code

if (DBNull.Value.Equals(o) || o != null)

where o is object o in row.ItemArray I keep getting an error of -->

Xml type "List of xdt:untypedAtomic" does not support a conversion from Clr type "DBNull" to Clr type "String".

What I don't understand is that when I step through my code this if should be catching this and performing my alternate action but it does not?

Can someone please shed some light for me.

Thank you!

Refracted Paladin
  • 12,096
  • 33
  • 123
  • 233
  • can you post some more code? where does o get assigned? – hackerhasid Jan 13 '10 at 21:03
  • 1
    What is the result of `Console.WriteLine(o.GetType().Name);`? – jason Jan 13 '10 at 21:10
  • 2
    i think it is a bit strange first you compare with DBNull and then with not null – Yevhen Jan 13 '10 at 21:18
  • You probably have a mistake somewhere else. – SLaks Jan 13 '10 at 21:36
  • 2
    Note that in many situations, database nulls do not behave like other objects. A database null represents the concept "I don't know". When you say "is x, which I don't know, equal to y, which I also don't know", the sensible answer is not "yes". The sensible answer is "I don't know". That's why in VB, comparing null to null results in null, not true or false. You might be running into a similar situation here. – Eric Lippert Jan 14 '10 at 00:14

3 Answers3

7

Try using

Convert.IsDBNull method.

SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
6

I think you problem is that in fact

DBNull.Value == null 
//is always false

The DBNull is a special class for comparisons on values returned from the dB so you actualy need to check for a null condition AND a DBNull.value if your array contains both.

EDIT: Sorry looking closer at your code you may just need to reverse your OR operation. If o == null your first statement will blow up with your exception. Try:

if (o != null || o == DBNull.Value) 
Tj Kellie
  • 6,336
  • 2
  • 31
  • 40
  • this edit is wrong, with it doing an exact opposite test of what is meant. As @ThomasLevesque correctly states, it needs to be `if (o == null || o == DBNull.Value)` instead. But the first statement also shudn't be a problem, but that style does lead to the problem of many coding errors where it will throw exceptions when the object is null. so it is *very* good to always check for null or not null *first*, before doing anything else with the value. – Shawn Kovac Feb 08 '16 at 14:51
  • 1
    the errors that 'blow up code' is when you try something like `if (o.ToString()) { ... }` because then when o is null, any *properties* or method functions like '.ToString()' will throw an error. so it's always good practice to make sure to test for null or not null first. However `if (DBNull.Value.Equals(o) || o != null)` is not accessing any property of o. And if the method handles a null value correctly, then there's no bug when o is null. But MANY programmers forget to test for null, so passing null to MANY methods frequently causes many problems because, honestly, a lot of code is sloppy. – Shawn Kovac Feb 08 '16 at 15:00
1

may be such comparison helps

if ( !o.GetType().Equals( DBNull.Value ) )

or

if (o is DBNull)
Yevhen
  • 1,897
  • 2
  • 14
  • 25