How can I retrieve information from deleted rows. I have deleted some rows from table in dataset, then I use method GetChanges(DataRowState.Deleted) to get deleted rows. I tried deleted rows in original table on server side, but it finished with these 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();
}
}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With