none
Deleted row information cannot be accessed through the row RRS feed

  • Question

  • My Dev Environment: VS2008
    Target Framework: .NET 2.0

    I have following piece of code:

                    dataSet.OperatorForeColors.DefaultView.Sort = "OperatorID, RegionID";

                    FranMapDataSet.OperatorForeColorsRow row = null;
                    foreach (DataRowView rowView in dataSet.OperatorForeColors.DefaultView)
                    {
                        row = (FranMapDataSet.OperatorForeColorsRow)rowView.Row;
                        // if (row.RowState == DataRowState.Deleted)
                        //    continue;

                        if (row.OperatorID == prevOperatorID)
                        {
                            row.Delete();
                        }
                        else
                        {
                            row.Initials = ExtractInitials(row.Operator);
                            prevOperatorID = row.OperatorID;
                        }

                    }
                    dataSet.OperatorForeColors.AcceptChanges();

    The above code is to remove duplicate Operators (only in memory). It works fine on my dev machine. But I got "Deleted row information cannot be accessed through the row" on the test and user's machine. (only has .NET 2.0).

    I do noticed in some posts that it is not a good idea to delete row in the foreach loop. But how it works on my dev machine?
    Wednesday, June 3, 2009 8:01 PM

Answers

  • As for the commented code. I added it because of the problem. It did help get rid of the error message, BUT it gave me more trouble - the Delete() was on the wrong records and the duplicates were not removed.

    So I decided NOT to delete in the loop and it worked for me. Following are the working pieces.

                    dataSet.OperatorForeColors.DefaultView.Sort = "OperatorID, RegionID";

                    FranMapDataSet.OperatorForeColorsRow row = null;
                    List<FranMapDataSet.OperatorForeColorsRow> rowsToBeDeleted = new List<FranMapDataSet.OperatorForeColorsRow>(); 
                    foreach (DataRowView rowView in dataSet.OperatorForeColors.DefaultView)
                    {
                        row = (FranMapDataSet.OperatorForeColorsRow)rowView.Row;

                        if (row.OperatorID == prevOperatorID)
                        {
                            rowsToBeDeleted.Add(row); // Remember it instead of delete in the loop
                        }
                        else
                        {
                            row.Initials = ExtractInitials(row.Operator);
                            prevOperatorID = row.OperatorID;
                        }

                    }

                    // Now delete them (or to be exact, Mark as delelted)
                    foreach (FranMapDataSet.OperatorForeColorsRow rowToBeDeleted in rowsToBeDeleted)
                    {
                        rowToBeDeleted.Delete();
                    }
                    rowsToBeDeleted.Clear();

                    dataSet.OperatorForeColors.AcceptChanges();

    Now the only thing troubles me is that why my old code was working in my Dev Machine (With .net 3.5).

    Thursday, June 4, 2009 3:27 AM

All replies

  • Make sure that those lines to skip rows that are already deleted are not commented out.  That would be the likely problem.

    You are right, deleting rows that you are iterating over with foreach is a "no no".  Put them in a List of rows to be deleted.  Then, once the foreach has ended, do a foreach over your List and remove from the DataSet.  In this way, you are not deleting from the same collection that you are iterating over.
    Wednesday, June 3, 2009 11:47 PM
  • As for the commented code. I added it because of the problem. It did help get rid of the error message, BUT it gave me more trouble - the Delete() was on the wrong records and the duplicates were not removed.

    So I decided NOT to delete in the loop and it worked for me. Following are the working pieces.

                    dataSet.OperatorForeColors.DefaultView.Sort = "OperatorID, RegionID";

                    FranMapDataSet.OperatorForeColorsRow row = null;
                    List<FranMapDataSet.OperatorForeColorsRow> rowsToBeDeleted = new List<FranMapDataSet.OperatorForeColorsRow>(); 
                    foreach (DataRowView rowView in dataSet.OperatorForeColors.DefaultView)
                    {
                        row = (FranMapDataSet.OperatorForeColorsRow)rowView.Row;

                        if (row.OperatorID == prevOperatorID)
                        {
                            rowsToBeDeleted.Add(row); // Remember it instead of delete in the loop
                        }
                        else
                        {
                            row.Initials = ExtractInitials(row.Operator);
                            prevOperatorID = row.OperatorID;
                        }

                    }

                    // Now delete them (or to be exact, Mark as delelted)
                    foreach (FranMapDataSet.OperatorForeColorsRow rowToBeDeleted in rowsToBeDeleted)
                    {
                        rowToBeDeleted.Delete();
                    }
                    rowsToBeDeleted.Clear();

                    dataSet.OperatorForeColors.AcceptChanges();

    Now the only thing troubles me is that why my old code was working in my Dev Machine (With .net 3.5).

    Thursday, June 4, 2009 3:27 AM
  • I think this is because the DataRowView maps per row position and you are changing the positions of the rows by your deletes.

    Avoid the use of DataView for this routine.  For example:

    foreach (DataRow row in dataSet.OperatorForeColors.Select("OperatorID, RegionID", "")
    {
    }

    Since Select returns a separate array, another benefit of this is that you will be able to do the deletes within the main foreach loop.
    Thursday, June 4, 2009 10:27 PM