How can I check whether a dataset is empty or not in

Questions by Ravinder Nain

Showing Answers 1 - 21 of 21 Answers


  • Feb 16th, 2006

What you mean is...



//do whatever


  Was this answer useful?  Yes


  • Feb 25th, 2006


HasRows property is only for DataReader objects.  For DataSet you can check whether it has a DataTable or not .. since the DataSet is a collection of DataTable you can check whether it has loaded a table or not using the following code

DataSet dt=new DataSet();

if (dt.Tables.Count>0) // has tables in it



else // otherwise it is empty




  • Mar 2nd, 2006

first two are wrong there is nothing a property like DataSet.HasRows in dataset

third one is right answer to say

  Was this answer useful?  Yes


  • Apr 3rd, 2006

All above answers are wrong.


DataSet_objectName.Tables[0].Rows.Count > 0

  Was this answer useful?  Yes


  • Apr 6th, 2006

the above ans is wrong as dataset is empty , it will throw error while acessing the first table . so the third is correct answere.

  Was this answer useful?  Yes


  • Apr 17th, 2006


Checking the dataset like:

if(DataSetObj.Tables[0].Rows.Count> 0)

is right only if you use another "if" statement above it, which is:

if(DataSetObj.Tables.Count > 0)


Ujjwal Prakash

  • Apr 21st, 2006

Absolutly Right..

To check that if a dataset has data or not we will have to first check if it has any table or not and then we ll have to look into the tables to check the row count.


if (ds.Tables.Count==0)


Console.Write("Empty Dataset");




 oreach(DataTable dt in ds)


  Was this answer useful?  Yes


  • Apr 25th, 2006

I think we have an method like IsNothing.Using that we can find That dataset has some data or notike :-- DataSet ds = new DataSet() if not IsNothing(ds) { TRUE block;}

  Was this answer useful?  Yes


  • May 16th, 2006

None of the answer seems completly correct. First check if(dataset== null)

then check of dataset.Tables == null if these checks are not made exception will occur when u check dataset.Tables.count or dataset.Tables[0].Rows.Count


  • May 24th, 2006


Forget all answers this is check and true

DataSet ds=new DataSet();

if(ds.Tables.Count ==0)


MessageBox.Show("There are "+ds.Tables.Count.ToString()+"Tables");





MessageBox.Show("There are "+ds.Tables.Count.ToString()+"Tables");


  Was this answer useful?  Yes


  • Jun 6th, 2006

u can directly comapre dataset to null  or counting the tables like in below case...

if(dsExample==null || dsExample.Tables.Count==0){ //empty dataset}

else{//not empty}

  Was this answer useful?  Yes


  • Jun 14th, 2011

Here it is folks:

  1. You need to firstly check to see if the dataset actually exists
  2. Then check if it has one or more tables
  3. Then check the row count for each table to see if any one table has data


            System.Data.DataSet data = new System.Data.DataSet();

            //data = bla bla get some data;

            bool dataFound = false;

            if( data != null && data.Tables.Count > 0 && data.Tables[ 0 ].Rows.Count > 0 )
                foreach( System.Data.DataTable table in data.Tables )
                    if( table.Rows.Count > 0 )
                        //we have data
                        dataFound = true;

            if( dataFound )
                Console.WriteLine( "we have data" );
                Console.WriteLine( "we have NO data" );

  Was this answer useful?  Yes


  • May 31st, 2012

Thank you Seeker911, your answer helped me :)

  Was this answer useful?  Yes


  • Nov 19th, 2012

  1. if(dataset.Tables.count!=0)

  2. {

  3. do some operation

  4. }

  5. else

  6. {

  7. nothing...

  8. }

  Was this answer useful?  Yes

Bucked Naked

  • Sep 11th, 2013

Wrong. Try this for better results:

  1. DataSet ds = <get a dataset somehow>;

  2. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows > 0)

  3. {

  4.     // There is something in the DataSet.

  5. }

  Was this answer useful?  Yes


  • Nov 16th, 2013

DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlString, sqlConn);

It is work i sure

  Was this answer useful?  Yes


  • Mar 11th, 2015

Check if dataset object is instanciated
Check uf it contains tables
Question is not to check whether it has records or not Question is if it is empty
Checking the row in first table does not help as there can exists more then one table

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions