Solved

how to handle empty datasets

Posted on 2006-06-14
5
235 Views
Last Modified: 2010-04-16
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
Comment
Question by:Paul Kahl
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 16904681
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
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 16904694
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
 
LVL 3

Author Comment

by:Paul Kahl
ID: 16904826
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
 
LVL 3

Author Comment

by:Paul Kahl
ID: 17061699
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question