error message on run

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prokopis
    New Member
    • Nov 2007
    • 30

    error message on run

    i have a small program that when i run it, it throw me an error exception.
    my code is
    Code:
    for (int columnumber = 0; columnumber <= columsize; columnumber++)
       {
          if (EpidemiologicalTable.Columns[columnumber].ColumnName.Equals(CodeName))
              {
                  for (int k = 0; k < updaterowcount; k++)//it search in the update table
                    {
                       for (int i = 0; i < rowsize; i++)//it search in the EpidemiologicalTable
                         {
                            string rowname = Convert.ToString(EpidemiologicalTable.Rows[i][0]);
                             int rowvalue = Convert.ToInt32(EpidemiologicalTable.Rows[i][columnumber]);
                              if (dataupdate.Rows[k][0].Equals(rowname))
                                 {
                                   DataRow selectedRow = dataupdate.Rows[k];
                                    if (rowvalue == 0)
                                     {
                                      MessageBox.Show("delete" + rowname + " " + rowvalue + " " + k);
                                      selectedRow.Delete();//remove the corrspoding row from the result table
                             
                                           }
                             
                                       }
                                   }
                               }
                             
                           }
                       }
    am trying to delete a certain row in the table but when i run it, ti give me the error
    "error deleted row information cannot be accessed through the row"
    can someone explain me why this certain error????
    Last edited by Plater; Mar 25 '08, 02:47 PM. Reason: code tags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You generally cannot delete a row when itterating through a collection like that (It would throw off the enumerator).

    What you could try is creating like an ArrayList, and adding the rows that need to be deleted to that when you find them.
    Then itterate through that and delete them? Its a tricky business, there is probably a better way though I am unaware of it.

    Comment

    • bluefootedpig
      New Member
      • Mar 2008
      • 7

      #3
      Originally posted by Plater
      You generally cannot delete a row when itterating through a collection like that (It would throw off the enumerator).

      What you could try is creating like an ArrayList, and adding the rows that need to be deleted to that when you find them.
      Then itterate through that and delete them? Its a tricky business, there is probably a better way though I am unaware of it.
      that generally only happens if the type is some sort of ienumerable. Like if he was using a foreach, in a for loop, it shouldn't be a problem, although you want to make sure after you delete a row you want to decrimate otherwise you will skip rows. example..

      data is:
      1,2,3,4,5

      if you iterate to and delete 3, then the 3rd spot is 4, but you haven't checked if 4 should be deleted and at the end of the loop you skip to 5.

      Comment

      Working...