iterating thru datatables collection

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
 
A

Alberto Poblacion

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) ...
 
P

Pete Kane

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)
 
M

Mark Peters

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top