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.
Solution 1. If a DataTable is null then you can't copy anything to it: there isn't anything to copy into!
You can also check if dataTable is empty using page.info() as described in this stackoverflow post. eg.
Preferred:
If dt Is Nothing Then ...
Is-Operator Nothingor (VB6 like)
If IsNothing(dt) Then ...
IsNothing Function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With