Kliver Max
Kliver Max

Reputation: 5299

How to handle System.DBNull?

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?

Upvotes: 1

Views: 2330

Answers (3)

Kjartan
Kjartan

Reputation: 19151

There is also a function for checking for DBNull:

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

Upvotes: 2

Nagaraj S
Nagaraj S

Reputation: 13484

Try this code

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

or

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

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

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.

Upvotes: 5

Related Questions