S Nash
S Nash

Reputation: 2499

How to compare a DBnull expression

I have the following code:

dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt")

Which has value of DBnull.

now I want to compare it:

if dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt")=nothing

....

...

I get

Run-time exception thrown : System.InvalidCastException - Operator is not valid for type 'DBNull' and 'Nothing'.

Ideas how to compare this value to see if it contains a DBnull?

if dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt")=system.DBnull also returns an error.

Upvotes: 1

Views: 812

Answers (5)

Victor Zakharov
Victor Zakharov

Reputation: 26434

Another option is to use Convert.DBNull (same as DBNull.Value):

If dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt") Is Convert.DBNull Then
  ' value is null
End If

I prefer this syntax, because it's very similar to Is Nothing comparison.

Upvotes: 2

A Coder
A Coder

Reputation: 3046

Try this:

`If IsDbNull() Then
 End If
`

Upvotes: 0

Boluc Papuccuoglu
Boluc Papuccuoglu

Reputation: 2356

DBNull.Value provides a way to do comparisons. So you can write:

if dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt")=DBNull.Value

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125640

Use IsDBNull function:

Returns a Boolean value that indicates whether an expression evaluates to the System.DBNull class.

If IsDBNull(dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt")) Then
    ' value is null
End If

Upvotes: 1

Dennis Traub
Dennis Traub

Reputation: 51644

You can either use the IsDBNull() function

If IsDbNull(dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt")) Then ...

or compare agains DBNull.Value

If dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt") Is DBNull.Value Then ...

Upvotes: 4

Related Questions