• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

how to handle empty datasets

I have a dataset populating a DataList, and it works beautifully, but it occurs to me that it's possible the dataset would have no results, and thus the datalist would be empty (tested, and the page just shows nothing where the datalist was). So the question arrises, how should I best handle the possibility of an empty dataset?

The dataset code is as follows:

public static DataSet getPartners()
    {
        SqlConnection SQLConn = new SqlConnection(ConnectionString());
        SqlCommand sqlCmd = new SqlCommand("spx_FetchPartners", SQLConn);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter sqlAdp = new SqlDataAdapter(sqlCmd);

        sqlCmd.Parameters.Add("@partner_id", SqlDbType.Int);
        sqlCmd.Parameters["@partner_id"].Value = 0;
        sqlCmd.Parameters.Add("@fetch_type", SqlDbType.Int);
        sqlCmd.Parameters["@fetch_type"].Value = 2;

        DataSet dtsData = new DataSet();

        try
        {
            SQLConn.Open();
            sqlAdp.Fill(dtsData);
        }
        finally
        {
            SQLConn.Close();
        }

        return dtsData;
    }

This in turn feeds into a small function in my .cs code-by-side:

public void getAllPartners()
    {
        dlPartnersMain.DataSource = MarCom.getPartners();
        dlPartnersMain.DataBind();
    }

which feeds the DataList in the front end.

C# solutions only, please.
0
Paul Kahl
Asked:
Paul Kahl
  • 2
  • 2
1 Solution
 
Ravi SinghSenior Software EngineerCommented:
Hi, I don't think theres a better way to check if its empty other than to iterate over the tables to determine if the row count is zero... you could try using the following method if theres no better solution:

      private static bool IsEmptyDataSet(DataSet dataSet)
      {
            if (dataSet == null || dataSet.Tables.Count == 0)
                  return true;
            else
            {
                  foreach (DataTable dt in dataSet.Tables)
                  {
                        if (dt.Rows.Count != 0)
                              return false;
                  }

                  return false;
            }
      }
0
 
Ravi SinghSenior Software EngineerCommented:
sorry, the last return should be a true:

      private static bool IsEmptyDataSet(DataSet dataSet)
      {
            if (dataSet == null || dataSet.Tables.Count == 0)
                  return true;
            else
            {
                  foreach (DataTable dt in dataSet.Tables)
                  {
                        if (dt.Rows.Count != 0)
                              return false;
                  }
                  return true;
            }
      }
0
 
Paul KahlAuthor Commented:
i'm a bit of a C# (and .Net) newb, so if it's not too much trouble, how would I go about incorporating that into my code-by-side function (getAllPartners())? I understand what it does, but I'm not sure how to implement.
0
 
Paul KahlAuthor Commented:
The answer was correct, tho the Expert never followed up the secondary part, I will award the points. I held the question open in the hopes that I would eventually see the full answer, but it was not to be.

The answer given does work, if one uses some creative work to incorporate it into their Data Layer schema.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now