Last post Apr 02, 2012 10:26 AM by WildAperture
Dec 05, 2007 03:13 AM|chris_dubois|LINK
I've been struggling with customizing my Select statement within my ObjectDataSource. At this point I'm just trying to delete all rows in my DataSet's DataTable that have a particular value in a column.
I've been trying this, but I get "Collection was modified" errors...
foreach (DataRow dr in table.Rows)
Thanks for any help.
Dec 06, 2007 03:35 AM|shrinidhi|LINK
you can not delete like that . try like this
Dec 06, 2007 04:30 AM|vinz|LINK
Each dr As DataRow
If (dr("Name") = "test" Then
Refer to this articles below
Dec 06, 2007 07:30 PM|chris_dubois|LINK
That totally makes sense, but it still doesn't work. I get an error saying the Collection was modified and the enumeration operation might not execute. I have the AcceptChanges call outside the loop, so I'm at a loss for why it would still have problems.
foreach (DataRow dr in ds.Tables.Rows)
if (dr["USEREDUCATIONID_STRING"] != USEREDUCATIONID_STRING)
Dec 06, 2007 07:45 PM|Suprotim Agarwal|LINK
That could be as you trying to remove items while looping.
Try a reverse loop instead :
Dec 06, 2007 08:12 PM|vinz|LINK
for (int i = 0; i < ds.Tables.Rows.Count; i++)
if (ds.Tables.Rows[i]["rowname"].ToString() == "nametobedeleted")
Nov 11, 2009 02:16 AM|qamarali|LINK
Hi Shrindhi there is little change the code. When I try to use it I got the error no record at the position 1 . That means after deleting if there is no record then it will error out. I fixed that . But my fix will work only to delete one record at a time.
Here is a change.
for (int i=(Ds.Tables["table_name"].Rows.Count-1); i >=0 ; i--)
string somename= Ds.Tables["table_name"].Rows[i]["Row_name"].ToString().Trim();
if(somename != "some string")
Break; <--- Here is the change
Qamar Ali Khan
Apr 02, 2012 10:26 AM|WildAperture|LINK
I had the same issue a while back. Then I realized when you remove a DataRow, the table indexes get updated as well.
while(i < table.Rows.Count)
i--; // this sets the index value back by 1
Hope it helps!