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)
{
if (!dr["USEREDUCATIONID_STRING"].ToString().Equals(USEREDUCATIONID_STRING))
table.Rows.Remove(dr);
}
table.AcceptChanges();
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[0].Rows)
{
if (dr["USEREDUCATIONID_STRING"] != USEREDUCATIONID_STRING)
{
dr.Delete();
}
}
ds.Tables[0].AcceptChanges();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i]["rowname"].ToString() == "nametobedeleted")
{
ds.Tables[0].Rows[i].Delete();
}
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")
{
Ds.Tables["table_name"].Rows.RemoveAt(i);
Break; <--- Here is the change
}
}
"There is no delight in owning anything unshared."
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")
{
Ds.Tables["table_name"].Rows.RemoveAt(i);
Break; <--- Here is the change
}
}
Regards
Qamar Ali Khan
"There is no delight in owning anything unshared."
chris_dubois
0 Points
2 Posts
Deleting Rows from DataTable
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) { if (!dr["USEREDUCATIONID_STRING"].ToString().Equals(USEREDUCATIONID_STRING)) table.Rows.Remove(dr); } table.AcceptChanges();shrinidhi
Member
434 Points
60 Posts
Re: Deleting Rows from DataTable
Dec 06, 2007 07:35 AM|LINK
Hi Chris,
you can not delete like that . try like this
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"){
Ds.Tables["table_name"].Rows.RemoveAt(i);
}
}
Regards,
Shri
Cheers,
Shri
vinz
All-Star
127067 Points
17946 Posts
MVP
Re: Deleting Rows from DataTable
Dec 06, 2007 08:30 AM|LINK
For Each dr As DataRow In ds.Tables(0).Rows
If (dr("Name") = "test" Then
dr.Delete()
End If
ds.Tables(0).AcceptChanges()
Next
OR
Refer to this articles below
http://msdn2.microsoft.com/en-us/library/feh3ed13(VS.80).aspx
http://www.c-sharpcorner.com/UploadFile/mahesh/DeleteDataTableRows08242006123435PM/DeleteDataTableRows.aspx
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
chris_dubois
0 Points
2 Posts
Re: Deleting Rows from DataTable
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[0].Rows) { if (dr["USEREDUCATIONID_STRING"] != USEREDUCATIONID_STRING) { dr.Delete(); } } ds.Tables[0].AcceptChanges();Thanks for any help!Suprotim Aga...
All-Star
15533 Points
1973 Posts
MVP
Re: Deleting Rows from DataTable
Dec 06, 2007 11:45 PM|LINK
Hi,
That could be as you trying to remove items while looping.
Try a reverse loop instead :
for (int rwCnt = ds.Tables[0].Rows.Count -1; rwCnt >= 0; rwCnt--){
ds.Tables[0].Rows.RemoveAt[rwCnt];
}
ds.Tables[0].AcceptChanges();
HTH,
Suprotim Agarwal
-----
http://www.dotnetcurry.com
-----
Free Magazine for ASP.NET Developers
vinz
All-Star
127067 Points
17946 Posts
MVP
Re: Deleting Rows from DataTable
Dec 07, 2007 12:12 AM|LINK
Try
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i]["rowname"].ToString() == "nametobedeleted")
{
ds.Tables[0].Rows[i].Delete();
}
}
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
qamarali
Member
6 Points
3 Posts
Re: Deleting Rows from DataTable
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")
{
Ds.Tables["table_name"].Rows.RemoveAt(i);
Break; <--- Here is the change
}
}
Many Thanks
Qamar Ali
qamarali
Member
6 Points
3 Posts
Re: Deleting Rows from DataTable
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")
{
Ds.Tables["table_name"].Rows.RemoveAt(i);
Break; <--- Here is the change
}
}
Regards
Qamar Ali Khan
Many Thanks
Qamar Ali
WildAperture
Member
2 Points
1 Post
Re: Deleting Rows from DataTable
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.
My solution:
int i=0;
while(i < table.Rows.Count)
{
if (!dr[i]["USEREDUCATIONID_STRING"].ToString().Equals(USEREDUCATIONID_STRING))
{
table.Rows.RemoveAt(i);
i--; // this sets the index value back by 1
}
i++;
}
Hope it helps!