1

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.

S Nash
  • 2,363
  • 3
  • 34
  • 64

5 Answers5

4

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 ...
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
2

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.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
1

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
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

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

if dsParticippantInfo.Tables(0).Rows(0)("Contract_Amt")=DBNull.Value
Boluc Papuccuoglu
  • 2,318
  • 1
  • 14
  • 24
  • Well, C# is my "native language" so I'm not 100%, but using this link: http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 , I thought that `DBNull.Value.Equals(a)` was the equivalent of `a=DBNull.Value` (That would be `DBNull.Value==a` in C#) Was I wrong? – Boluc Papuccuoglu Sep 10 '13 at 19:26
0

Try this:

`If IsDbNull() Then
 End If
`
A Coder
  • 3,039
  • 7
  • 58
  • 129