Solved

Error trying to remove row from Datatable

Posted on 2007-04-10
4
238 Views
Last Modified: 2010-04-15
I am trying to remove a row from a DataTable..

The DataTable is stored in memory so removing this row dows not delete data from the database..

My code currently is


private void Delete_Record(int prodid)
    {
        DataTable dto = new DataTable();
        dto = ((DataTable)Session["prodDT"]);
        foreach (DataRow r in dto.Rows)
        {
            int id = Convert.ToInt32(r["ID"]);
            if (id == prodid)
            {
                dto.Rows.Remove(r);
            }
       }
    }

This give me an error

Collection was modified; enumeration operation might not execute.

Can someone help me out here..

0
Comment
Question by:Prysson
  • 3
4 Comments
 
LVL 15

Accepted Solution

by:
igor_alpha earned 500 total points
ID: 18886716
Hi Prysson,
The problem, that you try to modify Rows collection when its already was modified inside foreach loop.
Best way to solve proble is looping from last row to first, so rows would be removed from end of table to its start.

private void Delete_Record(int prodid)
    {
        DataTable dto = new DataTable();
        dto = ((DataTable)Session["prodDT"]);
for(int i = dto.Rows.Count - 1; i == 0; i--)
{
DataRow r = dto.Rows[i];
 int id = Convert.ToInt32(r["ID"]);
            if (id == prodid)
            {
                dto.Rows.RemoveAt(i);
            }

}
0
 

Author Comment

by:Prysson
ID: 18886942
Works great! Thanks!
0
 

Author Comment

by:Prysson
ID: 18887075
Oops spoke too soon..

Having a bit of a problem with the code.

It onl;yworks if there is only one row int he table

If there are multiple rows in teh table for some reason it doesnt run the for loop..set the i variable and then just skips to the end..
I
0
 

Author Comment

by:Prysson
ID: 18887104
Ah never mind I figured it out

for(int i = dto.Rows.Count - 1; i == 0; i--)

needs to be

for(int i = dto.Rows.Count - 1; i >= 0; i--)

Otherwise it only runs if i is 0

Thanks again for the help...


0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question