Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a Datatable is Null or Nothing

How can I check if a DataTable has never been set, meaning it will be Null or Nothing? I don't mean an 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 is lost in memory for some reason, dt.Rows.Count <> 0 will throw this exception:

Object reference not set to an instance of an object.

like image 973
Laurence Avatar asked Apr 26 '12 10:04

Laurence


People also ask

Can DataTable be null?

Solution 1. If a DataTable is null then you can't copy anything to it: there isn't anything to copy into!

How check DataTable is empty in jquery?

You can also check if dataTable is empty using page.info() as described in this stackoverflow post. eg.


1 Answers

Preferred:

If dt Is Nothing Then ...
  • Is-Operator
  • Nothing

or (VB6 like)

If IsNothing(dt) Then ...

IsNothing Function

like image 64
Tim Schmelter Avatar answered Sep 23 '22 18:09

Tim Schmelter