?
Solved

Error trying to remove row from Datatable

Posted on 2007-04-10
4
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 15

Accepted Solution

by:
igor_alpha earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 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