473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Deleting data in dataset

Hi all,

Using C# 1.1

I have a dataset loaded from an XML file. The idea being that if the app (a
winform app that relies on a webservice) is offline, then I save data
locally in XML and update the web service later.

So, when I update, I have something like...

DataSet siteLinks = new DataSet();
siteLinks.ReadXml(WorkingDirectory + @"\temp\SaveLinks.xml");

// Upload Link Data.
foreach (DataRow dr in siteLinks.Tables[0].Rows)
{
try
{
if (uploaddatatowebservice) // Saves data into webservice and
returns boolean
{
// Remove the row once it has been processed.
dr.Delete();
}
else
{
MessageBox.Show("There was a problem saving the link. If problems
persist, please contact the administrator.");
}
}
catch(Exception ex)
{
string exc = ex.Message;
}
}

if (siteLinks.HasChanges())
{
siteLinks.AcceptChanges();
}
However, I get a problem... dr is the datarow from the dataset. I try and
delete the datarow (which seems to work, though is not actually deleted from
the XML at this point) but on the next iteration of the loop, I get a
System.InvalidOperationException Collection was modified; enumeration
operation may not execute.

What am I doing wrong?

Also, will what I have here actually save the changes back into the XML? If
not, what am I missing?

Thanks.
--
Best regards,
Dave Colliver.
http://www.LincolnFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Feb 21 '07 #1
8 3248
The error message suggests you are trying to modify the collection
through which you are iterating. And since you are deleting datarows
from siteLinks.Tables[0].Rows, this is what you are doing... changing
the iteration set.

One solution is to not directly delete the datarows during this first
pass. Instead, collect them in an ArrayList. Then after your first
pass, iterate through this ArrayList which holds the DataRows to be
deleted, and delete them in this second loop.

===============
Clay Burch
Syncufsion, Inc.

Feb 21 '07 #2
As for the exception switch from a foreach to a for where you start at the
last element and work forward or when you delete a record you decrement the
counter. GetEnumerator does not support deletes during the iteration.

"ClayB" wrote:
The error message suggests you are trying to modify the collection
through which you are iterating. And since you are deleting datarows
from siteLinks.Tables[0].Rows, this is what you are doing... changing
the iteration set.

One solution is to not directly delete the datarows during this first
pass. Instead, collect them in an ArrayList. Then after your first
pass, iterate through this ArrayList which holds the DataRows to be
deleted, and delete them in this second loop.

===============
Clay Burch
Syncufsion, Inc.

Feb 21 '07 #3
You never serialized back to the XML. (siteLinks.WriteXml(WorkingDirectory +
@"\temp\SaveLinks.xml");)

You accept changes which will be disposed once the DataSet object goes out
scope.

"David" wrote:
Hi all,

Using C# 1.1

I have a dataset loaded from an XML file. The idea being that if the app (a
winform app that relies on a webservice) is offline, then I save data
locally in XML and update the web service later.

So, when I update, I have something like...

DataSet siteLinks = new DataSet();
siteLinks.ReadXml(WorkingDirectory + @"\temp\SaveLinks.xml");

// Upload Link Data.
foreach (DataRow dr in siteLinks.Tables[0].Rows)
{
try
{
if (uploaddatatowebservice) // Saves data into webservice and
returns boolean
{
// Remove the row once it has been processed.
dr.Delete();
}
else
{
MessageBox.Show("There was a problem saving the link. If problems
persist, please contact the administrator.");
}
}
catch(Exception ex)
{
string exc = ex.Message;
}
}

if (siteLinks.HasChanges())
{
siteLinks.AcceptChanges();
}
However, I get a problem... dr is the datarow from the dataset. I try and
delete the datarow (which seems to work, though is not actually deleted from
the XML at this point) but on the next iteration of the loop, I get a
System.InvalidOperationException Collection was modified; enumeration
operation may not execute.

What am I doing wrong?

Also, will what I have here actually save the changes back into the XML? If
not, what am I missing?

Thanks.
--
Best regards,
Dave Colliver.
http://www.LincolnFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Feb 21 '07 #4
Thanks Josh,

I have done a reverse for loop and that now does not error... However, I do
have another problem... My code now looks like...
for (int Count = siteLinks.Tables[0].Rows.Count-1; Count >= 0; Count --)
{
DataRow dr = siteLinks.Tables[0].Rows[Count];
try
{
if (uploadedtowebservice)
{
// Remove the row once it has been processed.
dr.Delete();
}
}
catch(Exception ex)
{
string exc = ex.Message;
}
}

if (siteLinks.HasChanges())
{
siteLinks.AcceptChanges();
}

but the siteLinks.HasChanges is not set for some reason.

Once I have deleted the row from the datarow, I need to save the changes, so
that I don't end up reading it again the next time I run through the loop.

Thanks.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Josh" <Jo**@discussions.microsoft.comwrote in message
news:4A**********************************@microsof t.com...
As for the exception switch from a foreach to a for where you start at the
last element and work forward or when you delete a record you decrement
the
counter. GetEnumerator does not support deletes during the iteration.

"ClayB" wrote:
>The error message suggests you are trying to modify the collection
through which you are iterating. And since you are deleting datarows
from siteLinks.Tables[0].Rows, this is what you are doing... changing
the iteration set.

One solution is to not directly delete the datarows during this first
pass. Instead, collect them in an ArrayList. Then after your first
pass, iterate through this ArrayList which holds the DataRows to be
deleted, and delete them in this second loop.

===============
Clay Burch
Syncufsion, Inc.


Feb 21 '07 #5
David,

I think that the problem is that you are trying to delete a row while you
are still inside of the foreach loop. The safer method of doing this would
seem to me to be to build a collection of rows that you want to delete and
delete them after the ds has been iterated through.
The other option would be to iterate through the index of the rows in
reverse order with a regular for next loop rather than a foreach.

"David" wrote:
Hi all,

Using C# 1.1

I have a dataset loaded from an XML file. The idea being that if the app (a
winform app that relies on a webservice) is offline, then I save data
locally in XML and update the web service later.

So, when I update, I have something like...

DataSet siteLinks = new DataSet();
siteLinks.ReadXml(WorkingDirectory + @"\temp\SaveLinks.xml");

// Upload Link Data.
foreach (DataRow dr in siteLinks.Tables[0].Rows)
{
try
{
if (uploaddatatowebservice) // Saves data into webservice and
returns boolean
{
// Remove the row once it has been processed.
dr.Delete();
}
else
{
MessageBox.Show("There was a problem saving the link. If problems
persist, please contact the administrator.");
}
}
catch(Exception ex)
{
string exc = ex.Message;
}
}

if (siteLinks.HasChanges())
{
siteLinks.AcceptChanges();
}
However, I get a problem... dr is the datarow from the dataset. I try and
delete the datarow (which seems to work, though is not actually deleted from
the XML at this point) but on the next iteration of the loop, I get a
System.InvalidOperationException Collection was modified; enumeration
operation may not execute.

What am I doing wrong?

Also, will what I have here actually save the changes back into the XML? If
not, what am I missing?

Thanks.
--
Best regards,
Dave Colliver.
http://www.LincolnFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Feb 21 '07 #6
Thank you. I will give this a go. I didn't know if the accept changes would
have done this implicitly. Obviously not.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Josh" <Jo**@discussions.microsoft.comwrote in message
news:AA**********************************@microsof t.com...
You never serialized back to the XML. (siteLinks.WriteXml(WorkingDirectory
+
@"\temp\SaveLinks.xml");)

You accept changes which will be disposed once the DataSet object goes out
scope.

"David" wrote:
>Hi all,

Using C# 1.1

I have a dataset loaded from an XML file. The idea being that if the app
(a
winform app that relies on a webservice) is offline, then I save data
locally in XML and update the web service later.

So, when I update, I have something like...

DataSet siteLinks = new DataSet();
siteLinks.ReadXml(WorkingDirectory + @"\temp\SaveLinks.xml");

// Upload Link Data.
foreach (DataRow dr in siteLinks.Tables[0].Rows)
{
try
{
if (uploaddatatowebservice) // Saves data into webservice and
returns boolean
{
// Remove the row once it has been processed.
dr.Delete();
}
else
{
MessageBox.Show("There was a problem saving the link. If problems
persist, please contact the administrator.");
}
}
catch(Exception ex)
{
string exc = ex.Message;
}
}

if (siteLinks.HasChanges())
{
siteLinks.AcceptChanges();
}
However, I get a problem... dr is the datarow from the dataset. I try and
delete the datarow (which seems to work, though is not actually deleted
from
the XML at this point) but on the next iteration of the loop, I get a
System.InvalidOperationException Collection was modified; enumeration
operation may not execute.

What am I doing wrong?

Also, will what I have here actually save the changes back into the XML?
If
not, what am I missing?

Thanks.
--
Best regards,
Dave Colliver.
http://www.LincolnFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Feb 21 '07 #7
AcceptChanges only tracks changes to a row. If you delete a row there is
nothing to track changes on thus nothing to accept.

This is an example why they require you to write back to the source
explicitly. There are scenarios where you may not want the DataSet to auto
write back to the Data Source. You would call WriteXML in this particulair
case.

Hope this all makes sense.

"David" wrote:
Thanks Josh,

I have done a reverse for loop and that now does not error... However, I do
have another problem... My code now looks like...
for (int Count = siteLinks.Tables[0].Rows.Count-1; Count >= 0; Count --)
{
DataRow dr = siteLinks.Tables[0].Rows[Count];
try
{
if (uploadedtowebservice)
{
// Remove the row once it has been processed.
dr.Delete();
}
}
catch(Exception ex)
{
string exc = ex.Message;
}
}

if (siteLinks.HasChanges())
{
siteLinks.AcceptChanges();
}

but the siteLinks.HasChanges is not set for some reason.

Once I have deleted the row from the datarow, I need to save the changes, so
that I don't end up reading it again the next time I run through the loop.

Thanks.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Josh" <Jo**@discussions.microsoft.comwrote in message
news:4A**********************************@microsof t.com...
As for the exception switch from a foreach to a for where you start at the
last element and work forward or when you delete a record you decrement
the
counter. GetEnumerator does not support deletes during the iteration.

"ClayB" wrote:
The error message suggests you are trying to modify the collection
through which you are iterating. And since you are deleting datarows
from siteLinks.Tables[0].Rows, this is what you are doing... changing
the iteration set.

One solution is to not directly delete the datarows during this first
pass. Instead, collect them in an ArrayList. Then after your first
pass, iterate through this ArrayList which holds the DataRows to be
deleted, and delete them in this second loop.

===============
Clay Burch
Syncufsion, Inc.



Feb 21 '07 #8
Yeah the limatation here is GetEnumerator calls. foreach relies on the
IEnumerable interface function GetEnumerator. This method will throw an
exception if the count of items changes in the underlying collection.

The most efficent option:
-Use a for loop which updates on a delete the counter state. If your moving
forwards. You should decrement the counter after you delete. It you moving
backwards you should increment the counter after a delete.

What code smith is proposing is doable. It is just not the most efficent.

"Codesmith" wrote:
David,

I think that the problem is that you are trying to delete a row while you
are still inside of the foreach loop. The safer method of doing this would
seem to me to be to build a collection of rows that you want to delete and
delete them after the ds has been iterated through.
The other option would be to iterate through the index of the rows in
reverse order with a regular for next loop rather than a foreach.

"David" wrote:
Hi all,

Using C# 1.1

I have a dataset loaded from an XML file. The idea being that if the app (a
winform app that relies on a webservice) is offline, then I save data
locally in XML and update the web service later.

So, when I update, I have something like...

DataSet siteLinks = new DataSet();
siteLinks.ReadXml(WorkingDirectory + @"\temp\SaveLinks.xml");

// Upload Link Data.
foreach (DataRow dr in siteLinks.Tables[0].Rows)
{
try
{
if (uploaddatatowebservice) // Saves data into webservice and
returns boolean
{
// Remove the row once it has been processed.
dr.Delete();
}
else
{
MessageBox.Show("There was a problem saving the link. If problems
persist, please contact the administrator.");
}
}
catch(Exception ex)
{
string exc = ex.Message;
}
}

if (siteLinks.HasChanges())
{
siteLinks.AcceptChanges();
}
However, I get a problem... dr is the datarow from the dataset. I try and
delete the datarow (which seems to work, though is not actually deleted from
the XML at this point) but on the next iteration of the loop, I get a
System.InvalidOperationException Collection was modified; enumeration
operation may not execute.

What am I doing wrong?

Also, will what I have here actually save the changes back into the XML? If
not, what am I missing?

Thanks.
--
Best regards,
Dave Colliver.
http://www.LincolnFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

Feb 21 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Hi All, When I delete the last record from my dataset and then WriteXML() to the file; the Table itself, in the xml file, is removed. I need to prevent the table structure from being deleted if...
1
by: Junkguy | last post by:
I'm having difficulty deleting rows from a datagrid. I want to put a "delete" button on a form and achieve the same functionality as hitting the "delete" key on the keyboard for the selected row of...
5
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
2
by: NDady via DotNetMonster.com | last post by:
Hi, I have a datagrid populated from a dataset. On every row in the grid I have a delete button. When the user presses on the delete button I remove the row from the dataset and rebind the...
0
by: Robert Batt | last post by:
Hello I have a problem deleting rows from a datagrid that is bound to a dataset. The datagrid updates and inserts just fin so the binding to the dataset must be ok. However when I try to delete a...
5
by: Robert Brown | last post by:
Hi All. I have a routine that checks a SQL Table for all records 3 months prior to a predetermined date, then I insert them into an Archive DB then delete those records from the original table....
3
by: marshallarts | last post by:
Hello, I have a datagrid (grdShots) on a form, and a button to allow the user to delete records from the dataset underlying the grid. My code appears to work, because the row disappears from...
12
by: Randy | last post by:
Hi, Trying to pass along a table row delete to the datasource, but I'm crashing. Here is the code: Private Sub btnDeleteIngr_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
2
by: emphyrio | last post by:
Hi, I am new to programming in vb. net. I have this problem: I created a form with a datagrid. I can add records and then save them to a XML file, using this code on the click event of a button...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.