How do you test for an empty dataset

M

Mike P

I have written a simple web service that basically takes a value input
by a user and returns a dataset with all related data found in a
database.

In my client app use this code to get the data from the web service :

Dataset dsData = ws.wsmethod(parameter);

And whether any data is returned or not, I don't get an error, so I need
to test if the dataset is empty or not to see if any data has been
returned.

Can anybody help me out with this?


Cheers,

Mike
 
J

JV

Mike, question 1 is whether the returned dataset reference can be null. If
so, you need to check for that first.

If not, then be aware that a DataSet contains a collection of DataTable
objects. For each DataTable object, you can access the Rows collection and
check its Count property.
 
M

Mike P

JV,

Using if (ds == null) doesn't seem to work, so I'm having to use this
syntax to check if the Dataset is empty or not :

string str = dsTransResult.Tables["tbl"].Rows.Count.ToString();

Is this the standard way of doing this, it seems a bit long winded just
to check for an empty dataset?


Cheers,

Mike
 
P

Peter Rilling

You got it right. (Although not sure that the ToString() is for in this
context.)

You could always wrap this in some utility/helper method.
 
J

Joe Fawcett

Peter Rilling said:
You got it right. (Although not sure that the ToString() is for in this
context.)

You could always wrap this in some utility/helper method.

Mike P said:
JV,

Using if (ds == null) doesn't seem to work, so I'm having to use this
syntax to check if the Dataset is empty or not :

string str = dsTransResult.Tables["tbl"].Rows.Count.ToString();

Is this the standard way of doing this, it seems a bit long winded just
to check for an empty dataset?


Cheers,

Mike
Do you need to go that far, can't you just test the dataset's Tables.Count? Or
might it have tables that are present but of no use?
 
G

Guest

if you are looking to see if the Data set object is empty ie null or has no
tables, then
ds.Tables["my table name "] with throw an exception if "my table name" is
not in your data set.
So what you need to do is check if ds is null, then see if the
ds.Tables.count >0, see if it has your table ds.Tables.Contains("my table
name"), now you can look at the table and see if it has any rows
ds.Tables["my table name"].Rows.Count >0
Aaron
 
H

Hans Kesting

Joe said:
You got it right. (Although not sure that the ToString() is for in this
context.)

You could always wrap this in some utility/helper method.

JV,

Using if (ds == null) doesn't seem to work, so I'm having to use this
syntax to check if the Dataset is empty or not :

string str = dsTransResult.Tables["tbl"].Rows.Count.ToString();

Is this the standard way of doing this, it seems a bit long winded just
to check for an empty dataset?


Cheers,

Mike

Do you need to go that far, can't you just test the dataset's Tables.Count? Or
might it have tables that are present but of no use?

Tables.Count counts the number of "tables". There probably is at least
one table, but that might be empty. Tables[0].Rows.Count counts the
number of actual rows in the first table (and ignores any other tables
that might be present).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top