Question

I have a object with type:

dynamic {System.DBNull}

I want to check it:

if (myObject!= null || myObject!= DBNull.Value)
{
   MessageBox.Show("Oh hi");
}

But the MessageBox always appears. Whats wrong, is it another type?

Was it helpful?

Solution

This expression is always true

myObject != null || myObject != DBNull.Value

because myObject cannot be null and DBNull.Value at the same time. Replace || with && to fix.

OTHER TIPS

Try this code

if(myObject != DBNull.Value)
{
   MessageBox.Show("Oh hi");
}

or

if(myObject != null && myObject != DBNull.Value)
{
   MessageBox.Show("Oh hi");
}

There is also a function for checking for DBNull:

if(myObject != null && !Convert.IsDBNull(myObject))
{
   MessageBox.Show("Oh hi");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top