Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Apr 02, 2012 02:26 PM by WildAperture
Dec 05, 2007 07:13 AM|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 07:35 AM|LINK
you can not delete like that . try like this
Dec 06, 2007 08:30 AM|LINK
Each dr As DataRow
If (dr("Name") = "test" Then
Refer to this articles below
Dec 06, 2007 11:30 PM|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 11:45 PM|LINK
That could be as you trying to remove items while looping.
Try a reverse loop instead :
Dec 07, 2007 12:12 AM|LINK
for (int i = 0; i < ds.Tables.Rows.Count; i++)
if (ds.Tables.Rows[i]["rowname"].ToString() == "nametobedeleted")
Nov 11, 2009 06:16 AM|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 02:26 PM|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!