Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to handle empty datasets

Posted on 2006-06-14
5
Medium Priority
?
250 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 1500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

705 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