Tashi Tobgay
Tashi Tobgay

Reputation: 191

Checking IsDBNull using if statement

The code only checks if condition, it avoids else condition, anything i'm doing wrong while using IsDBNull?. If the "if" condition fails, i want it to go to else condition.

while (rd.Read())
{
    if (!rd.IsDBNull(0))
    {
        //update table
    }
    else
    {
        //update table 
    }
}

Can anyone help me on this thanks

Upvotes: 0

Views: 192

Answers (2)

Kjartan
Kjartan

Reputation: 19151

Are you sure your record even has any rows? If not, it might appear as if your if is failing, when in fact, it is never being called.

You can check this using the property HasRows.

Upvotes: 1

David Pilkington
David Pilkington

Reputation: 13618

An if statement will always go to the else if the condition is false.

I cannot see your table so I can only speculate that for ever row read, the first column (usually a PK) is not null.

Remember, that loop will stop once there are no longer any rows to read. And what you are checking is if the first column is null (which, as you can see, it never is)

The if statement works and has always worked.

Upvotes: 1

Related Questions