GeekInterview.com
Series: Subject: Topic:
Question: 133 of 460

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

Asked by: Ravinder Nain | Member Since Jan-2006 | Asked on: Feb 2nd, 2006

View all questions by Ravinder Nain

Showing Answers 1 - 18 of 18 Answers
anup2006

Answered On : Feb 3rd, 2006

View all answers by anup2006

if Dataset.HasRows=true then

dataset contains data otherwise empty

  
Login to rate this answer.
Gavin

Answered On : Feb 16th, 2006

What you mean is...

if(Dataset.HasRows)

{

//do whatever

}

  
Login to rate this answer.
Nandagopal

Answered On : 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

{

}

Yes  2 Users have rated as useful.
  
Login to rate this answer.

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

third one is right answer to say

  
Login to rate this answer.
Guest

Answered On : Mar 2nd, 2006

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

third one is right answer to say

  
Login to rate this answer.
inxs

Answered On : Apr 3rd, 2006

All above answers are wrong.

Use,

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

  
Login to rate this answer.
prakhar

Answered On : 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.

  
Login to rate this answer.
Omar

Answered On : 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
Ujjwal Prakash

Answered On : 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)

{}

  
Login to rate this answer.
Srinivas

Answered On : 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;}

  
Login to rate this answer.
Dhejo

Answered On : 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
omi

Answered On : 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");

}

  
Login to rate this answer.
Shipra

Answered On : 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}

  
Login to rate this answer.
John Jiang

Answered On : Jul 2nd, 2008

View all answers by John Jiang

IsNothing is only used in VB or VB.NET. someone is really confused

  
Login to rate this answer.
Sanjida123

Answered On : Jun 3rd, 2010

View all answers by Sanjida123

Check if (dataset != null), and then work on dataset.

  
Login to rate this answer.
Seeker911

Answered On : Jun 14th, 2011

View all answers by Seeker911

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

  
Login to rate this answer.
Anais

Answered On : May 31st, 2012

Thank you Seeker911, your answer helped me :)

  
Login to rate this answer.
Rajavel

Answered On : Nov 19th, 2012

Code
  1. if(dataset.Tables.count!=0)
  2. {
  3. do some operation
  4. }
  5. else
  6. {
  7. nothing...
  8. }

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.