Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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

Posted

A dataset has tables is in it - you need to check all the tables for no rows. Your sql just returns an empty cursor but still causes there to be a table. What you need to do is:

 


bool dsEmpty = true;
int tbCount = 0;

while(dsEmpty && tbCount < dsChg.Tables.Count)
{
   if(dsChg.Tables[tbCount].Rows.Count > 0)
      dsEmpty = false;
   else
      tbCount++;
}

//Now dsEmpty holds whether the ds is 'empty' or not.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...