Click here to Skip to main content
15,887,356 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Anything that i did did'nt help, tried to put an
C#
int x=DataBase.ds.Tables.Count;
if(x>0)
{ 
//rest of code
}

The problem is mainly in the for, and does'nt happen in any other forms(while using Tables[0])...
I'm not describing about the content because its not relevant, I need a solution that works in any code.

What I have tried:

C#
<pre>    

                {
                    for (int i1 = 0; i1 < DataBase.ds.Tables[0].Rows.Count; i1++)
                    {


                        if (//some condition)
                        {
                              //some code
                        }
                    }
                }
Posted
Updated 29-Dec-19 5:52am
Comments
Richard MacCutchan 29-Dec-19 4:16am    
Are you certain that the dataset has been initialised correctly at that point?

Better to check for all these

C#
if(ds != null)                  // see if data set is not null
if(ds.Tables.Count > 0 )        // see if your data set has tables
if(ds.Tables[0].Rows.Count > 0)  //see if first table in your data set has any rows


Or you can create a generic function to see if your dataset is Empty like

C#
bool IsEmpty(DataSet dataSet)
{
    return !dataSet.Tables.Cast<DataTable>().Any(x => x.DefaultView.Count > 0);
}


also The Fill() method of DataAdapter returns the # of rows added for the first table in the DataSet.
 
Share this answer
 
Based on the error, the data set has no tables when you execute the for loop. Since you try to use the first table (index 0) from the dataset you get the error message.

So use the debugger to go through the code which populates the dataset. Check for example if
- Populating code is omitted because of a condition
- The for loop is called before population occurs
- When populated no data is found etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900