Krev32 Krev32 - 1 year ago 50 Question

Check if 2 databases exist

This is probably a newbie question however how can you check if two datatable are empty? If one datatable has records and the other does not do something. Or if they both have records do something. If none has records do nothing.

If dt is nothing andalso dt.rows.count > 0 andalso dt2 is nothing andalso dt2.rows.count > 0 then

' Process

End if

Answer Source

Sometimes splitting the code in more simple parts could be a solution to avoid complex if conditions

Dim firstEmptyOrNull = dt is Nothing OrElse dt.Rows.Length = 0
Dim secondEmptyOrNull = dt2 is Nothing OrElse dt2.Rows.Length = 0

If firstEmptyOrNull And secondEmptyOrNull Then
  ' Do nothing 
Else if Not firstEmptyOrNull And secondEmptyOrNull Then
  ' Code if the first table is good but not the second one
Else if firstEmptyOrNull And Not secondEmptyOrNull Then
  ' Code if the second table is good but not the first one
  ' Code for both tables good
End If