How to check if Datatable Null or Nothing

How to check that the DataTable parameter has never been set, that is, it will be Null or Nothing ? I do not mean an empty DataTable .

For instance:

 Dim dt As DataTable = TryCast(Session("dt"), DataTable) If dt.Rows.Count <> 0 Then 'Do something ! End If 

If for some reason Session("dt") never set or lost in memory, dt.Rows.Count <> 0 will throw this exception:

The reference to the object is not installed in the instance of the object.

+6
source share
2 answers

Preferred:

 If dt Is Nothing Then ... 

or (VB6)

 If IsNothing(dt) Then ... 

Isnothing function

+12
source

If the value data type is varbinary (MAX), use

 if dt.rows(0).item(2) Is DBNull.Value then... 
0
source

Source: https://habr.com/ru/post/914211/


All Articles