Solved

Error trying to remove row from Datatable

Posted on 2007-04-10
4
241 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 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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