How to tell if a DataSet is null in ASP.net C# ?

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

How to we tell if a DataSet is null in a ASP.Net C# Windows Form?
Is it possible that a DataSet is not null but has no DataTable?
What will be the simplest code?
Thanks for help.

Jason
 
You can always check your object for a null value.

DataSet ds;
....
if ( ds == null ) ...

Or if it is not null, you can check for tables in it:

if ( ds.Tables.Count == 0 ) ... // error


-Lenard
 
Hi Jason,

It's possible that a DataSet is not null but without any DataTable in it.
For example, you instance a DataSet:

DataSet ds = new DataSet();

The ds is not null, but there is no DataTable in it. Even if there are
DataTables in a DataSet, maybe there is not any data in it. See following
code:

DataTable tbl = new DataTable();
ds.Tables.Add(tbl);

The DataTable, tbl, is an empty one.

HTH

Elton Wang
 
You can always check your object for a null value.

DataSet ds;
...
if ( ds == null ) ...

Or if it is not null, you can check for tables in it:

if ( ds.Tables.Count == 0 ) ... // error

All in a row:

if (( ds != null ) && ( ds.Tables.Count > 0 ))
// ok
else
// error


Note that

if (( ds.Tables.Count > 0 ) && ( ds != null ))

won't work ;)
 
Back
Top