lawphotog lawphotog -4 years ago 189 Question

How to check if a Datatable is Null or Nothing

How can I check if a datatable has never been set ? or Null or Nothing ? I don't mean empty Datatable ... For example,

Dim dt As DataTable = TryCast(Session("dt"), DataTable)

If dt.Rows.Count <> 0 Then
Do something !
End If

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

Object reference not set to an instance of an object.

So how do i prevent happening this ?

Thanks a lot ..


Answer Source


If dt Is Nothing Then ...

or (VB6 like)

If IsNothing(dt) Then ...

IsNothing Function

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download