-1

Possible Duplicate:
Error: Deleted row information cannot be accessed through the row. (C#)

I get this error when im trying to delete a row from my DataSet:

Deleted row information cannot be accessed through the row

How do i fix this.

Here is my function below:

    private void btnDelete_Click(object sender, EventArgs e)
    {
       var result =  MessageBox.Show("Proceed to deleting this location?", "Delete Location", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
       if (result == DialogResult.Yes)
       {
            var row = myData.xspArea.FindByxar_Id(pRow.xar_Id);
            if (row.RowState != DataRowState.Deleted)
            {
                row.Delete();
                MessageBox.Show("This location has been deleted", "Location Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
       }
       else
       {
          this.Close();
       }
    }
Community
  • 1
  • 1
floormind
  • 1,868
  • 5
  • 31
  • 85

3 Answers3

0

I think you need to call mData.AcceptChanges() after row.Delete().

EDIT:

As @Tim Schmelter says: AcceptChanges will prevent updates in database since the rowstate is what the datadapter uses to determine what to do with the row

Yaqub Ahmad
  • 27,569
  • 23
  • 102
  • 149
  • 1
    `AcceptChanges` will prevent updates in database since the rowstate is what the datadapter uses to determine what to do with the row. – Tim Schmelter Nov 13 '12 at 15:26
0

It might be worth reading the MSDN documentation to understand what row.Delete() is supposed to do.

If the RowState of the row is Added, the RowState becomes Detached and the row is removed from the table when you call AcceptChanges.

The RowState becomes Deleted after you use the Delete method on an existing DataRow. It remains Deleted until you call AcceptChanges. At this time, the DataRow is removed from the table.

A deleted row can be undeleted by invoking RejectChanges.

So, row.Delete() will change the row.RowState to DataRowState.Deleted.

If you want your row to actually be deleted from the DataSet, you need to call row.AcceptChanges().

kdrvn
  • 1,009
  • 1
  • 7
  • 10
0

I assume that the exception is actually raised on FindByxar_Id(pRow.xar_Id) and not later on row.Delete();. Have you created a copy of the DataRow with pRow or is that row also deleted(maybe through cascade delete constraints)? Then you cannot access the xar_Id because it's already deleted.

So check this also:

// ...
if(pRow.RowState != DataRowState.Deleted)
{
    var row = myData.xspArea.FindByxar_Id(pRow.xar_Id);
    if (row.RowState != DataRowState.Deleted)
    {
        row.Delete();
        // ...
    }
// ...

If you want to update the database you need to use a DataAdapter.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939