Reputation: 2499
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
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
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
Reputation: 125640
Use IsDBNull
function:
Returns a
Boolean
value that indicates whether an expression evaluates to theSystem.DBNull
class.
If IsDBNull(dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt")) Then
' value is null
End If
Upvotes: 1
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