Saturday 18 August 2012

How to check your Dataset or Datatable is null or empty

Here I will explain to you , how to check dataset or datatable is null or empty.

If you want to check your dataset or datatable is empty or not ,you have to wright some code as given below.

C# code:

SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
// Condition to check if dataset tables contains data or not
if (ds.Tables[0].Rows.Count > 0)
{
  Label1.Text= "Dataset table contains data";
}
}
else
{
  Label1.Text= "Dataset does not contains data";
}

And for DataTable:



SqlDataAdapter da=new SqlDataAdapter(cmd);
DataTable dt = new DataTable ();
da.Fill(dt);
// Condition to check if dataset tables contains data or not
if (dt.Rows.Count > 0)
{
  Label1.Text= "DataTable contains data";
}
}
else
{
  Label1.Text= "DataTable does not contains data";
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.