Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / April 2006

Tip: Looking for answers? Try searching our database.

foreach behavior changed in Framework 2.0

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rich Wood - 26 Apr 2006 18:26 GMT
I have the following code in a C# project:

DataView dv = new DataView( dsModels.Tables[_INDVARS_TABLE_NAME], "", "",
DataViewRowState.CurrentRows);
foreach ( DataRowView drv in dv)
    drv.Row.Delete();

This code worked fine in VS 2003 (Framework 1.1). However in VS 2005
(Framework 2.0) it behaves differently. It looks to me that the call to
drv.Row.Delete() actually deletes the next item in the list. I tested this by
creating two foreach loops, the first just outputs an ordinal value that gets
incremented each time through the loop and a unique id of the row; the second
loop outputs the same information and subsequently deletes the row with the
drv.Row.Delete() statement. I put a try/catch block around the output
statement in the second loop and in the catch block I output the ordinal
value and the error.

number of items in the dataview: 13
0: id = 861
1: id = 961
2: id = 966
3: id = 1089
4: id = 3194
5: id = 40
6: id = 3190
7: id = 3193
8: id = 3195
9: id = 422
10: id = 3192
11: id = 2477
12: id = 768
0: id = 861
1: id = 966
2: id = 3194
3: id = 3190
4: id = 3195
5: id = 3192
6: id = 768
7: error = There is no row at position 7.
8: error = There is no row at position 8.
9: error = There is no row at position 9.
10: error = There is no row at position 10.
11: error = There is no row at position 11.
12: error = There is no row at position 12.

It appears that the first call to drv.Row.Delete() actually deletes the row
with id 961 (the "next" row) rather than the row with id 861 (the "current"
row). To make things worse, it tries to keep iterating past the end which is
where the error messages are coming from.

I could not find any documentation regarding this behavior but I wasn't sure
exactly where to look. Any insight would be appreciated.

Thanks
Mark Ashton - 26 Apr 2006 22:05 GMT
This is a known issue, I don't believe there is a KB article on it.

The workaround is something like
while(0 < dv.Count)  { dv[dv.Count-1].Row.Delete(); }

notes:
1.  depending on the filter and expressions, deleting a row can actually
add/remove rows from a DataView.
2.  while(0 < dv.Count) { dv[0].Row.Delete(); } has performance issues in V2.0

> I have the following code in a C# project:
>
[quoted text clipped - 50 lines]
>
> Thanks
Kevin Yu [MSFT] - 27 Apr 2006 06:28 GMT
Hi Rich,

Yes, this is a known issue in .NET 2.0. It is by design and seems to be a
break change in version 2.0.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.