How to get information from a deleted row

How to get information from deleted rows. I deleted several rows from the table in the dataset, then I use the GetChanges (DataRowState.Deleted) method to get deleted rows. I tried deleted rows in the source table on the server side, but ended up with these errors.

System.Data.DeletedRowInaccessibleException: information about the deleted row could not be accessed through the row.

What is right? Here is my code, any advice? Thanks to everyone.

    Dataset ds = //get dataset from client side
    //get changes
    DataTable delRows = ds.Tables[0].GetChanges(DataRowState.Deleted);

    //try delete rows in table in DB
                    if (delRows != null)
                    {
                        string connStr = WebConfigurationManager.ConnectionStrings["Employees"].ConnectionString;
                        conn = new SqlConnection(connStr);

                        conn.Open();

                        for (int i = 0; i < delRows.Rows.Count; i++)
                        {
                            string cmdText = string.Format("DELETE Tab1 WHERE Surname=@Surname");

                            cmd = new SqlCommand() { Connection = conn, CommandText = cmdText };

//here is problem, I need get surnames from rows which was deleted
                            var sqlParam = new SqlParameter(@"Surname", SqlDbType.VarChar) { Value = delRows.Rows[i][1].ToString() };
                            cmd.Parameters.Add(sqlParam);


                            cmd.CommandText = cmdText;
                            cmd.Connection = conn;

                            cmd.ExecuteNonQuery();
                        }
                    }
+5
source share
1 answer

You need to indicate that you want to see the original version of DataRow. The default is used DataRowVersion.Current.

Value = delRows.Rows[i][1, DataRowVersion.Original].ToString()
+6
source

All Articles