How to access deleted rows in a typed dataset?

Sometimes after doing some processing on a dataset and before finally committing the changes in the database we need to know some information about added, modified, deleted or dispatched rows in the dataset. One of the well known approaches  is to loop the rows of a data set and check the ‘RowState’ value of each row which could be added, modified, deleted or dispatched. But when you are using a transaction in your code then you will notice that after updating the changes in database and before committing the transaction you will lose the deleted rows although you will still be able to get the rowstate of added and modified rows. This was irritating for me as i wanted to get the count of deleted rows in my code. The fortunately i came accross another nicer method of accessing added, modified, deleted and dispatched rows which is Table.GetChanges(‘RowsState’). In this way you can separately make tables for each kind of rows and then use them for your purpose. Here is an example of ‘Customer’ table.

 DataTable tableAddedRows, tableModifiedRows, tableDeletedRows, tableDispatchedRows;

tableAddedRows = myDataSet.tables[“Customer”].Rows()[0].Table.GetChanges( DataRowState.Added );
tableModifiedRows = myDataSet.tables[“Customer”].Rows()[0].Table.GetChanges( DataRowState.Modified);
tableDeletedRows = myDataSet.tables[“Customer”].Rows()[0].Table.GetChanges( DataRowState.Deleted );
tableDispatchedRows = myDataSet.tables[“Customer”].Rows()[0].Table.GetChanges( DataRowState.Dispatched);

Now you can use these tables to do whatever you want. Please note that if there is no row of any one of these rowstates then its corresponding table will be null.

Ref : Zee Malik


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s