How to test if a DataSet is empty?
C#ado.netDatasetC# Problem Overview
I'm modifying someone else's code where a query is performed using the following:
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlString, sqlConn);
da.Fill(ds);
How can I tell if the DataSet is empty (i.e. no results were returned)?
C# Solutions
Solution 1 - C#
If I understand correctly, this should work for you
if (ds.Tables[0].Rows.Count == 0)
{
//
}
Solution 2 - C#
You don't have to test the dataset.
The Fill()
method returns the # of rows added.
Solution 3 - C#
It is not a valid answer as it gives following error
>Cannot find table 0.
Use the following statement instead
if (ds.Tables.Count == 0)
{
//DataSet is empty
}
Solution 4 - C#
You should loop through all tables and test if table.Rows.Count is 0
bool IsEmpty(DataSet dataSet)
{
foreach(DataTable table in dataSet.Tables)
if (table.Rows.Count != 0) return false;
return true;
}
Update: Since a DataTable could contain deleted rows RowState = Deleted
, depending on what you want to achive, it could be a good idea to check the DefaultView
instead (which does not contain deleted rows).
bool IsEmpty(DataSet dataSet)
{
return !dataSet.Tables.Cast<DataTable>().Any(x => x.DefaultView.Count > 0);
}
Solution 5 - C#
We can check total three ways.
if(ds != null)
if(ds.Tables.Count > 0 )
if(ds.Tables[0].Rows.Count > 0)
Solution 6 - C#
To check dataset is empty or not You have to check null and tables count.
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlString, sqlConn);
da.Fill(ds);
if(ds != null && ds.Tables.Count > 0)
{
// your code
}
Solution 7 - C#
This code will show an error like
Table[0] can not be found!
because there will not be any table in position 0.
if (ds.Tables[0].Rows.Count == 0)
{
//
}
Solution 8 - C#
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
MessageBox.Show("No result found");
}
query will receive the data in data set and then we will check the data set that is it empty or have some data in it. for that we do ds.tables[0].Rows.Count == o this will count the number of rows that are in data set. If the above condition is true then the data set ie ds is empty.
Solution 9 - C#
Don't forget to set table name da.Fill(ds,"tablename");
So you return data using table name instead of 0
if (ds.Tables["tablename"].Rows.Count == 0)
{
MessageBox.Show("No result found");
}
Solution 10 - C#
When returning results from SQL query I find that tables[0] exists, but it has zero rows. So in my situation this worked:
if (ds.Tables[0].Rows.Count == 0) //empty
This did not work:
if (ds.Tables.Count == 0)
Solution 11 - C#
Fill
is command always return how many records inserted into dataset.
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlString, sqlConn);
var count = da.Fill(ds);
if(count > 0)
{
Console.Write("It is not Empty");
}
Solution 12 - C#
You can use something like this
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
//Code
}
Solution 13 - C#
A much better way is to use the following:
ds.Tables.Count == 0
As the ds.Tables[0].Rows.Count == 0
can give the error:
System.IndexOutOfRangeException: 'Cannot find table 0.'
Solution 14 - C#
This should work
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlString, sqlConn);
da.Fill(ds);
if(ds.Tables.Count > 0)
{
// enter code here
}
Solution 15 - C#
If (ds != null)
Should do the work for you!