By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 2,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

How do you test for an empty dataset

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
"Mike P" <mi*******@gmail.com> wrote in message
news:ej*************@TK2MSFTNGP12.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 17 '05 #2

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #3

P: n/a
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" <mi*******@gmail.com> wrote in message
news:OB*************@TK2MSFTNGP12.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 17 '05 #4

P: n/a
"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
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" <mi*******@gmail.com> wrote in message
news:OB*************@TK2MSFTNGP12.phx.gbl...
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?

--

Joe (MVP)

https://mvp.support.microsoft.com/pr...8-8741D22D17A5
Nov 17 '05 #5

P: n/a
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

"Mike P" wrote:
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 17 '05 #6

P: n/a
Joe Fawcett wrote:
"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
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" <mi*******@gmail.com> wrote in message
news:OB*************@TK2MSFTNGP12.phx.gbl...
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).
--
Hans Kesting
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.