Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Error trying to remove row from Datatable

Posted on 2007-04-10
4
Medium Priority
?
243 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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