Determine if Dataset is empty [C#]
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Determine if Dataset is empty [C#]

  1. #1
    Join Date
    Oct 2004
    Posts
    429

    Question Determine if Dataset is empty [C#]

    My goal - I need to search a database (EXCEL file) using typical "SELECT" statements (as shown below) to determine if a matching value exists (stored in the dataset).
    If the Dataset (ds) is EMPTY or ==NULL then I need to do something specific... So I wrote the following code:

    Code:
    System.Data.DataSet dsChg = oExcel.Read("select * from [Task$] where STATUS<>'FINISHED' and CLIENTS='" + cbClient.Text + "' and ASSIGNMENTS='" + cbAssignment.Text + "'");
    if (dsChg != null)
    	{	// Repopulate
    		... do some work
    	}
    Note that oExcel is an object that allows me to communicate with my EXCEL [.xls] file which is acting as a database.
    So, I am checking to see if there is a row that has STATUS != FINISHED, and CLIENTS & ASSIGNMENTS = to the current values in the corresponding comboboxes

    Problem I am having - even if I am 100% sure the Dataset is empty (meaning if I populate a datagrid with the Dataset there are no values/results) BUT the "if (dsChg != null) isn't working - the dataset is evaluated as if it wasn't empty.
    This is causing me a lot of problems because if it is not empty than I assume that row(0) has values and when I try to access them I get errors because there is nothing in row(0) - shouldn't that mean the Dataset is EMPTY?

    So I need a better/more accurate/correct way to determine if the Dataset is empty or not (meaning did it find a match in the Excel Database).
    Any help/hints/comments would be much appreciated, Thanks

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Determine if Dataset is empty [C#]

    You should test how many tables/rows the DataSet contains before trying to access the tables/rows.
    Code:
    DataSet dataSet = ...
    if (dataSet.Tables.Count == 0)
    {
         // there are no tables in the result
    }
    
    if (dataSet.Tables[0].Rows.Count == 0)
    {
        // there are no rows in the first table
    }
    - petter
    I love deadlines. I like the whooshing sound they make as they pass by - Douglas Adams.
    Visit me!.

    Use code-tags! [code]Your code here[/code]

  3. #3
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Determine if Dataset is empty [C#]

    Quote Originally Posted by Shatin00
    So I need a better/more accurate/correct way to determine if the Dataset is empty or not
    Code:
     if(ds!=null || ds.Tables.Count!=0 ||
    	ds.Tables[0].Rows.Count!=0)
    because of compiler optimization there would be no null reference exception,it first check the ds!=null if it's true won't check the rest.

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Determine if Dataset is empty [C#]

    Quote Originally Posted by mehdi62b
    Code:
     if(ds!=null || ds.Tables.Count!=0 || ds.Tables[0].Rows.Count!=0)
    because of compiler optimization there would be no null reference exception,it first check the ds!=null if it's true won't check the rest.
    This would absolutely cause null-pointer exceptions. If ds == null it would continue to check the next condition, and then throw a null pointer exception.

    Code:
    if(ds==null || ds.Tables.Count ==0 || ds.Tables[0].Rows.Count ==0)
    {
        // no data in data set
    }
    - petter
    I love deadlines. I like the whooshing sound they make as they pass by - Douglas Adams.
    Visit me!.

    Use code-tags! [code]Your code here[/code]

  5. #5
    Join Date
    Sep 2004
    Location
    Tehran(Ir)
    Posts
    469

    Re: Determine if Dataset is empty [C#]

    well,my mistake
    Quote Originally Posted by wildfrog
    Code:
    if(ds==null || ds.Tables.Count ==0 || ds.Tables[0].Rows.Count ==0)
    {
    // no data in data set
    }
    I don't think there would be any need for the second condition,if there is no schema,ds would be null
    Code:
    if(ds==null || ds.Tables[0].Rows.Count ==0)
    {
    // no data in data set
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center