One guy asked me a question, he was having problems in updating and deleting rows when looping through a DataTable rows.
A typical scenario is, say you have to loop through a DataTable rows and do some calculations on the rows and delete some rows depending on the condition. For example, deleting rows with Name = Mahesh.
If you do like in the following code, you will get an error on AcceptChanges method and the reason is deleting rows inside the loop means affecting the indexing.
- ' Loop though the table
- For Each dr As DataRow In ds.Tables(0).Rows
- If (dr("Name") = "Mahesh" Then
- dr.Delete()
- End If
- ds.Tables(0).AcceptChanges()
- Next
However, if you mark the rows as delete using Delete method and accept changes on the DataTable outside the loop, you should be good to go. See the following code:
- ' Loop though the table
- For Each dr As DataRow In ds.Tables(0).Rows
- If (dr("Name") = "Mahesh" Then
- dr.Delete()
- End If
- Next
- ds.Tables(0).AcceptChanges()

Rohan RaoPosted Apr 14, 2020, 1:46 AM
Good article sir. I have a question here..What if my columns are dynamic. Let's say date column which comes from database. If I want to update the particular date column with some specific string, how can we write it?
kalai romiPosted Jan 7, 2020, 11:29 PM
Good Article sir,,Is it possibe to attach a code for asp.net for this article ?
kalu singh raoPosted Jul 25, 2016, 2:58 AM
Thank you sir
yasharPosted Apr 19, 2011, 5:57 AM
Hi,I think there is better way(Must Be) to find some Row With Filtering...
Ken WilliamsPosted Nov 16, 2010, 1:20 PM
Collection was modified; enumeration operation might not execute.
satish kumarPosted Sep 16, 2010, 3:30 AM
Thanks dude, without AcceptChanges()my Datatable was showing errors .
Anamika SharmaPosted Mar 3, 2010, 1:43 AM
how to delete a row from sql server2005 database based on datagridview row selection at runtime?
mateiaPosted Jan 12, 2010, 3:44 AM
I get this runtime error: System.InvalidOperationException: Collection was modified; enumeration operation might not execute.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p> <o:p> </o:p> If I try this foreach (DSHelpBillingAssets.MPRNsRequiringBundlingRow row in dsHelp.MPRNsRequiringBundling.Rows)<o:p></o:p> {<o:p></o:p> if (row.Converter + row.AMR == 0)<o:p></o:p> {<o:p></o:p> row.Delete();<o:p></o:p> }<o:p></o:p> <o:p></o:p> }<o:p></o:p> dsHelp.MPRNsRequiringBundling.AcceptChanges();<o:p></o:p>
Omar AkhtarPosted Jul 20, 2009, 5:38 AM
Actually i have used a grid view that shows values from the datbase table created in SQL Server 2005. The thing I actually want is that I should select rows from the grid view and when I press the delete button the selected values from the grid view must be deleted along with values from the database table. All the code must be in C#.
Omar AkhtarPosted Jul 20, 2009, 5:36 AM
Actually i have used a grid view that shows values from the datbase table created in SQL Server 2005. The thing I actually want is that I should select rows from the grid view and when I press the delete button the selected values from the grid view must be deleted along with values from the database table. All the code must be in C#.
sasi kanthPosted Dec 15, 2007, 1:53 AM
error Update requires a valid InsertCommand when passed DataRow collection with new rows. my code is dim da as new OleDbDataAdapter("select * from Acces_reg", conn) dim ds as DataSet = new DataSet() da.Fill(ds, "Acces_reg") dim dr as DataRow = ds.Tables("Acces_reg").NewRow() dr(1)=.textbox1.text ------------------------ ds.Tables("Acces_reg").Rows.Add(dr) da.Update(ds, " Acces_reg")
saumik voraPosted Mar 17, 2007, 2:51 AM
Hi, I working on C#, .Net 1.0. I am getting broadcast from the server which gives me around 50 rows, which i capture it and store it into data table, which is bound to the grid. before storing it, i delete the data table and add new rows. The problem here is, that the grid flickers every time the feed comes from the broadcast. I thought to create an array of DataRow and add the same to the data table at a time how do i add the whole range of datarows into a data table at a time.