473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do you test for an empty dataset

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
6 19025
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andreas Palm | last post by:
I have a dataset that has DBNull in certain columns, now when I write out this one to XML, I only get the columns as elements that do have data in it. However I do need also the empty colums as...
2
by: Phil Galey | last post by:
How do you get around the problem of empty tables in a dataset not being included when you do a WriteXML? When you later read the data back into a new dataset, those tables are gone, because they...
2
by: Ron | last post by:
Can anyone tell me how to check and see if a dataset is empty? I am writing a search function that returns a dataset and binds it to a datagrid. If no data is found, I would like to make the grid...
3
by: martin | last post by:
Hi, I am having trouble determining if my dataset is empty. I have function that populates a dataset from a database and then retuens it. If the database contains no data then the dataset will...
22
by: EMW | last post by:
Hi, I managed to create a SQL server database and a table in it. The table is empty and that brings me to my next chalenge: How can I get the info in the table in the dataset to go in an empty...
6
by: Ben Finney | last post by:
Howdy all, Summary: I'm looking for idioms in unit tests for factoring out repetitive iteration over test data. I explain my current practice, and why it's unsatisfactory. When following...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
3
by: AAJ | last post by:
Hi all A quick question on Datasets..... I populate a dataset from a SQL SERVER 2000 database via a data adapter. I then use the dataset to populate member variables of my class. I know the...
2
by: Shum | last post by:
Hi! i have a question.. I'm filling a dataset from a table in which some rows are empty, i dont want those empty records to be filled in the dataset.. Does any one know how to restrict it to only...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.