Solved

how to handle empty datasets

Posted on 2006-06-14
5
217 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now