54

I have a DataSet where I need to find out how many rows has been changed using the following code:

dataTable1 = dataSet1.Tables["FooTable"].GetChanges();

foreach (DataRow dr in dataTable1)
{
  // ...
}

DataSet has DataSet.HasRow but DataTable doesn't have such method. If there is no changed rows. changedDT1 will be a null value, causing the loop to throw exception.

How do I check if DataTable is empty? I tried Rows.Count - doesn't work...

10 Answers 10

74

First make sure that DataTable is not null and than check for the row count

if(dt!=null)
{
  if(dt.Rows.Count>0)
  {
    //do your code 
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

You don't need to check for a row count, the foreach will simply not run if there are no rows.
@Ben, that is my original question - when there are no rows the loop through me an exception because what's looping is a Null.
@KMC the point is not that the loop throws not because there are no rows, it is that the variable is null, it does not exist, you can't add rows to it, it can never have any rows, it can't be looped through, it is null. If it is not null but is empty and has no rows then the loop will not throw an exception.
The datatable could NOT be null but still contain no rows. This solution is more complete as it checks for both.
A foreach loop will simply not iterate over an empty collection, explicitly checking if it's empty as well is redundant.
50

If dataTable1 is null, it is not an empty datatable.

Simply wrap your foreach in an if-statement that checks if dataTable1 is null. Make sure that your foreach counts over DataTable1.Rows or you will get a compilation error.

    if (dataTable1 != null)
    {
       foreach (DataRow dr in dataTable1.Rows)
       {
          // ...
       }
    }

1 Comment

You forgot to put dataTable1.Rows If not you get a compilation error. Please correct.
27

Normally when querying a database with SQL and then fill a data-table with its results, it will never be a null Data table. You have the column headers filled with column information even if you returned 0 records.When one tried to process a data table with 0 records but with column information it will throw exception.To check the datatable before processing one could check like this.

if (DetailTable != null && DetailTable.Rows.Count>0)

2 Comments

If you are databinding to a GridView and you returned 0 records but got the column headers, you will not have an exception thrown as the EmptyDataTemplate is applied to the GridView Control. In this case, checking just for null is fine.
this is the most elegant solution
15

Don't use rows.Count. That's asking for how many rows exist. If there are many, it will take some time to count them. All you really want to know is "is there at least one?" You don't care if there are 10 or 1000 or a billion. You just want to know if there is at least one. If I give you a box and ask you if there are any marbles in it, will you dump the box on the table and start counting? Of course not. Using LINQ, you might think that this would work:

bool hasRows = dataTable1.Rows.Any()

But unfortunately, DataRowCollection does not implement IEnumerable. So instead, try this:

bool hasRows = dataTable1.Rows.GetEnumerator().MoveNext()

You will of course need to check if the dataTable1 is null first. if it's not, this will tell you if there are any rows without enumerating the whole lot.

Comments

5

You can also simply write

 if (dt.Rows.Count == 0)
     {
         //DataTable does not contain records
     }        

2 Comments

But if the database is null, this will throw an exception.
Yes , you are right. Hence you will always need to perform a check if datable is NULL.
5

This is an old question, but because this might help a lot of c# coders out there, there is an easy way to solve this right now as follows:

if ((dataTableName?.Rows?.Count ?? 0) > 0)

Comments

4

As from MSDN for GetChanges

A filtered copy of the DataTable that can have actions performed on it, and later be merged back in the DataTable using Merge. If no rows of the desired DataRowState are found, the method returns Nothing (null).

dataTable1 is null so just check before you iterate over it.

Comments

1
Sub Check_DT_ForNull()
Debug.Print WS_FE.ListObjects.Item(1).DataBodyRange.Item(1).Value
If Not WS_FE.ListObjects.Item(1).DataBodyRange.Item(1).Value = "" Then
   Debug.Print WS_FE.ListObjects.Item(1).DataBodyRange.Rows.Count
End If
End Sub

This checks the first row value in the DataBodyRange for Null and Count the total rows This worked for me as I downloaded my datatable from server It had not data's but table was created with blanks and Rows.Count was not 0 but blank rows.

Comments

0

I use an extension. Maybe it is helpful.

 public static class DataTableExtensions
 {
     public static bool IsNullOrEmpty(this DataTable table) => table is null || table.Rows.Count == 0;
 }

Comments

-1
bool empty = !DataTableExtensions.AsEnumerable(DataTableToCheck).Any()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.