iterating thru datatables collection

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
i have a loop like the following:

foreach(DataTable dt in DataTablesCollection1)
{
if(dt[0][0].ToString().Contains("something")
{
//do something
}
}

the problem is when i get an empty datatable and it doesn't find a row at
the position in the if statement. how do you avoid something like that?

thanks,
rodchar
 
rodchar said:
hey all,
i have a loop like the following:

foreach(DataTable dt in DataTablesCollection1)
{
if(dt[0][0].ToString().Contains("something")
{
//do something
}
}

the problem is when i get an empty datatable and it doesn't find a row at
the position in the if statement. how do you avoid something like that?

Add a statement to check the number of rows:

if (dt.Rows.Count>0) ...
 
rodchar said:
hey all,
i have a loop like the following:

foreach(DataTable dt in DataTablesCollection1)
{
if(dt[0][0].ToString().Contains("something")
{
//do something
}
}

the problem is when i get an empty datatable and it doesn't find a row at
the position in the if statement. how do you avoid something like that?

thanks,
rodchar
put another if before

if(dt[0][0].ToString().Contains("something")

like

if(dt[0].Rows.Count > 0)
 
the problem is when i get an empty datatable and it doesn't find a row at
the position in the if statement. how do you avoid something like that?

if (dt.Rows.Count == 0) {// Do something else; continue;}

try/catch maybe?

How expected is an empty table?
What are you trying to do?
 
Back
Top