Solved

how to handle empty datasets

Posted on 2006-06-14
5
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

751 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