Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Data Tables with DBNULL values

Tags:

vb.net

I am pulling data from SQL into DataTables using the following query:

Select * FROM [dbname].INFORMATION_SCHEMA.COLUMNS 

I am comparing values from two DataTables using the following code:

If dtSource(iRow)(3) <> dtTarget(i)(3) Then
     'make moves
End If

I get the following error: Operator '<>' is not defined for type 'DBNull' and type 'DBNull'.

It is throwing the exception when comparing the COLUMN_DEFAULT column from INFORMATION_SCHEMA.COLUMNS, which has NULL values in it.

The way I'm handling this right now is by checking if there are any null values before comparing them:

If IsDBNull(dtSource(iRow)(3)) OR IsDBNull(dtTarget(i)(3)) Then 
     If (IsDBNull(dtSource(iRow)(3)) And NOT IsDBNull(dtTarget(i)(3))) OR viceversa.. Then 
          'make moves 
     End If
Else 
     If dtSource(iRow)(3) <> dtTarget(i)(3) Then
         'make moves
    End If
End If

I feel like there has to be a better way to do this, but I'm stuck on this. Any help is appreciated!

like image 997
cjw Avatar asked Mar 25 '26 02:03

cjw


1 Answers

Try :

   If Not IsDBNull(dtSource(iRow)(3)) AndAlso Not IsDBNull(dtTarget(iRow)(3)) AndAlso dtSource(iRow)(3) <> dtTarget(i)(3) Then
            'make moves 
        ElseIf IsDBNull(dtSource(iRow)(3)) Or IsDBNull(dtTarget(iRow)(3)) Then
            'make moves 
        Else
            'Failure
        End If
like image 99
Beldi Anouar Avatar answered Mar 26 '26 17:03

Beldi Anouar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!