You need hard work to get Lucky!

They are ill discoverers that think there is no land, when they can see nothing but sea.

Archive for March, 2010

How to access deleted rows in a typed dataset?

Posted by zeemalik on March 9, 2010


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.

I hope this helps!

Posted in | Leave a Comment »