Wednesday, June 20, 2012

Deleted row information cannot be accessed through the row.


The exception Deleted row information cannot be accessed through the row is thrown when we try to get the details form a DataRow, which is deleted from the DataTable. 

To get the details from the deleted row we have to add the parameter DataRowVersion.Original, to the Column Name/Index.

dtChanges = dtEmployee.GetChanges(DataRowState.Deleted);
if (dtChanges != null)
{
   objConn.Open();
   objCmd.Connection = objConn;
   for (int i = 0; i < dtChanges.Rows.Count; i++)
   {
      strQuery = "DELETE FROM Employee ";
      strQuery += "WHERE ID = '" + dtChanges.Rows[i]["ID", DataRowVersion.Original].ToString() + "'";
      //
      // Execute the Query.
      objCmd.CommandText = strQuery;
      objCmd.ExecuteNonQuery();
   }
   objConn.Close();
   dtChanges = null;
}

The
DataTable.GetChanges(DataRowState.Deleted), returns a DataTable which contains the detials of all the rows which were deleted in the DataGridView.

If we use dtChanges.Rows[i]["ID"], instead of dtChanges.Rows[i]["ID", DataRowVersion.Original], then the following exception will be thrown.

System.Data.DeletedRowInaccessibleException: Deleted row information cannot be accessed through the row.

Related Posts
DataRow RowState
DataTable AcceptChanges
DataTable RejectChanges
Get new rows in a DataTable / DataGridView in C# Windows Forms
Get Modified rows in a DataTable / DataGridView in C# Windows Forms
Get Deleted rows in a DataTable / DataGridView in C# Windows Forms


Search Flipkart Products:
Flipkart.com

No comments: