How can I check whether a dataset is empty or not in C#.net

Questions by Ravinder Nain

Showing Answers 1 - 35 of 35 Answers

Gavin

  • Feb 16th, 2006
 

What you mean is...

if(Dataset.HasRows)

{

//do whatever

}

  Was this answer useful?  Yes

Nandagopal

  • Feb 25th, 2006
 

Hi

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

{

}

Guest

  • 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

inxs

  • Apr 3rd, 2006
 

All above answers are wrong.

Use,

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

  Was this answer useful?  Yes

prakhar

  • 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

Omar

  • Apr 17th, 2006
 

Hi

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)

Regards

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.

eg:

if (ds.Tables.Count==0)

{

Console.Write("Empty Dataset");

}

else

{

 oreach(DataTable dt in ds)

{}

  Was this answer useful?  Yes

Srinivas

  • 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

Dhejo

  • 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

omi

  • May 24th, 2006
 

hi,

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");

}

sqlDataAdapter1.Fill(ds,"hello");

if(ds.Tables.Count>0)

{

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

}

  Was this answer useful?  Yes

Shipra

  • 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

Seeker911

  • 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

Example.

            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;
                        break;
                    }
                }
            }

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

  Was this answer useful?  Yes

Anais

  • May 31st, 2012
 

Thank you Seeker911, your answer helped me :)

  Was this answer useful?  Yes

Rajavel

  • Nov 19th, 2012
 

Code
  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:

Code
  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

Chandrashekhar

  • Nov 16th, 2013
 

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

It is work i sure

  Was this answer useful?  Yes

Deepak

  • 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