tags:

views:

84

answers:

1

How can I retrie infromation from delete rows. I delete some rows from table in dataset, then I use method GetChanges(DataRowState.Deleted) to get deleted rows. I try delete rows in original table on server side, but it finished with this errors.

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

What is correct way? Here is my code, any advice? Thank you everybody

    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();
                        }
                    }
A: 

You need to specify that you want to look at the original version of the DataRow. By default DataRowVersion.Current is used.

Value = delRows.Rows[i][1, DataRowVersion.Original].ToString()
BenV