Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check dataset is empty or not

Tags:

c#

dataset

This is working for me just fine. With if checks if dataset is empty or not. If so, return null value. But is the check of dataset right way or should i do some other way?

 da2 = new SqlDataAdapter("SELECT project_id FROM project WHERE _small_project_id = '" + cb_small_project.SelectedValue + "' ORDER BY NEWID()", conn);
 ds2 = new DataSet();
 da2.Fill(ds2);
 DataRow[] rowProject = dt2.Select();

 if (ds2.Tables[0].Rows.Count == 0)
    cmd.Parameters["@_project_id"].Value = guidNull;
 else
    cmd.Parameters["@_project_id"].Value = rowProject[0]["project_id"];
like image 813
JanOlMajti Avatar asked Feb 07 '12 08:02

JanOlMajti


People also ask

How do you check if a dataset is empty?

If the dataset exists but has zero observations, then the dataset is empty. You can check the number of observations with the COUNT function, the Dictionary Tables, the Descriptor Portion of a dataset, or with a macro function.

How do I check if a dataset is empty in Python?

empty attribute checks if the dataframe is empty or not. It returns True if the dataframe is empty else it returns False in Python.

How do you check if a dataset has a table or not?

Rows. Count != 0);This will return true if there are any rows in any of the tables. It will return false if there are no tables or no rows.


2 Answers

This Worked for me.... and won't give exception....

foreach (DataTable table in ds.Tables)
   if (table.Rows.Count != 0)
      table.Dispose();
like image 63
Osama Rizwan Avatar answered Oct 12 '22 13:10

Osama Rizwan


In my opinion the 'right' way is to check both:

ds2.Tables.Count 

ds2.Tables[0].Rows.Count
like image 22
Rafał Warzycha Avatar answered Oct 12 '22 11:10

Rafał Warzycha