G
Guest
i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For the
life of me i can't figure out how
Option 1: foreach
foreach (DataRow row in dataSet.MY_TABLE.Rows) {
row.Delete();
}
This doesn't work. It erases the first row and then throws the exception
Someone Changed The Enumerator. No fun
Option 2: Clear()
dataSet.MY_TABLE.Clear();
This wipes out all the rows. Unfortunately, when i go to save the changes
the rows don't show up. Here's the update code:
foreach (dataSet.DataRow row in table.GetChanges().Rows)
{
switch (row.RowState)
{
case DataRowState.Deleted:
//--- Never get here
Apparently Clear() doesn't update whatever it is that leads to GetChanges
and RowState
So those are what i've tried and have had no luck with. Other ideas?
In experimenting, it looks like perhaps the proper way to do these things is
to use for i=0 to Rows.Count. That will leave the rows where they are but two
things will happen. First, the RowState will get set to Deleted. Second, any
attempt to read a deleted row will throw an exception. This bothers me
because it seems like calling Delete from for i= and from foreach results in
different behavior - if it's just marking rows it doesn't seem like calling
Delete should have screwed up the enumerator
i think i can hobble my code together but does anyone have any insight as to
how this works and why?
-baylor
life of me i can't figure out how
Option 1: foreach
foreach (DataRow row in dataSet.MY_TABLE.Rows) {
row.Delete();
}
This doesn't work. It erases the first row and then throws the exception
Someone Changed The Enumerator. No fun
Option 2: Clear()
dataSet.MY_TABLE.Clear();
This wipes out all the rows. Unfortunately, when i go to save the changes
the rows don't show up. Here's the update code:
foreach (dataSet.DataRow row in table.GetChanges().Rows)
{
switch (row.RowState)
{
case DataRowState.Deleted:
//--- Never get here
Apparently Clear() doesn't update whatever it is that leads to GetChanges
and RowState
So those are what i've tried and have had no luck with. Other ideas?
In experimenting, it looks like perhaps the proper way to do these things is
to use for i=0 to Rows.Count. That will leave the rows where they are but two
things will happen. First, the RowState will get set to Deleted. Second, any
attempt to read a deleted row will throw an exception. This bothers me
because it seems like calling Delete from for i= and from foreach results in
different behavior - if it's just marking rows it doesn't seem like calling
Delete should have screwed up the enumerator
i think i can hobble my code together but does anyone have any insight as to
how this works and why?
-baylor