Solved

Error trying to remove row from Datatable

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now