473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

iterating thru datatables collection

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
Jun 27 '08 #1
4 996
"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:68**********************************@microsof t.com...
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) ...
Jun 27 '08 #2
rodchar wrote:
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)
Jun 27 '08 #3
>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?
Jun 27 '08 #4
thanks everyone for the help,
rod.

"rodchar" wrote:
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
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Patrick von Harsdorf | last post by:
I want to iterate over a collection and delete all unwanted entries. for item in collection: del item of course doesn´t do anything useful. Two things come to mind: a) iterating backwards...
1
by: chris yoker via DotNetMonster.com | last post by:
hiya, I add new nodes to an xmlDoc. I want to add "innerText" to every newly-created node. current XML file <code> <rows> <row> <PRODUCT-TYPE>bike</PRODUCT-TYPE>...
2
by: Jenny K | last post by:
Hi everyone, I've got a console application that grabs data into 3 datatables asynchronously, and writes each datatable's data to a textfile. The problem is that the data returned by each sproc...
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
2
by: Anushya | last post by:
Hi when i try to browse thru all the folders in outlook recursively starting from the root folder, i couldnot explore the USER FOLDERS which comes straight under root folder. Wot is the...
10
by: Ken Foster | last post by:
I have a hashtable keyed by some name, holding an instance of an object. Most of the time I use the hashtable in the traditional sense, given a name, I lookup the object, then I run a method on...
2
by: David Veeneman | last post by:
Is there a way to iterate the components on a form? I need to determine whether an instance of my custom component (a System.ComponentModel component) is present in a form. I have tried...
0
by: JDMils | last post by:
I am having trouble finding the AutoNumber field of my database with this code. The code is used to replicate a specific table, reproducing all columns including indexes and Primary Keys (there is...
21
by: MLH | last post by:
Am having trouble with the following snippet. It stops after only cycling thru open forms. There are many more than that. How to modify this to cycle thru all forms? Sub AllOpenForms() Dim...
5
by: mikehulluk | last post by:
Ok, Imagine I have a class class C { }; ostream& operator<<(ostream& o, const C& c) { ...}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.