Counting Records added and modified in a DataSet
-
Hi Everyone: Cleako showed me a link where I can return records that were either added to or modified in a DataSet. What I would like to do is show the user a message giving them the number of records that where added to the dataset and a number of existing records that where modified before they commit update to the database. I didn't see anything like - MyDataSet.GetChanges(DataRowState.Modified.Count) or MyDataSet.GetChanges(DataRowState.Added.Count) which would make sense but isn't in the DataRowState class. Anyone have an idea on how to accomplish this? Thanks, Quecumber256
-
Hi Everyone: Cleako showed me a link where I can return records that were either added to or modified in a DataSet. What I would like to do is show the user a message giving them the number of records that where added to the dataset and a number of existing records that where modified before they commit update to the database. I didn't see anything like - MyDataSet.GetChanges(DataRowState.Modified.Count) or MyDataSet.GetChanges(DataRowState.Added.Count) which would make sense but isn't in the DataRowState class. Anyone have an idea on how to accomplish this? Thanks, Quecumber256
The answer is staring you in the face. What does
DataSet.GetChanges()
return?? Another DataSet object! A DataSet is a collection of DataTables, which is a collection of DataRow objects. You can get the number of tables in this new DataSet quite easily:Dim changes As DataSet = MyDataSet.GetChanges(DataRowState.Modified) If changes IsNot Nothing AndAlso Not changes.HasErrors Then ' Get the number of tables in this changed dataset. Dim numTablesChanged As Integer = changes.Tables.Count ' Enumerate through these tables and add up the number of rows in each. Dim numTotalRecordChanges As Integer For Each t As DataTable In changes.Tables numTotalRecordChanges += t.Rows.Count Next End If
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
The answer is staring you in the face. What does
DataSet.GetChanges()
return?? Another DataSet object! A DataSet is a collection of DataTables, which is a collection of DataRow objects. You can get the number of tables in this new DataSet quite easily:Dim changes As DataSet = MyDataSet.GetChanges(DataRowState.Modified) If changes IsNot Nothing AndAlso Not changes.HasErrors Then ' Get the number of tables in this changed dataset. Dim numTablesChanged As Integer = changes.Tables.Count ' Enumerate through these tables and add up the number of rows in each. Dim numTotalRecordChanges As Integer For Each t As DataTable In changes.Tables numTotalRecordChanges += t.Rows.Count Next End If
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007Dave: If I read your example right you are sending the DataSet of only those records that where modified? So your example will show the total number of records in the dataset that where modified(changed)? FYI - I'm so wet behind the ears in Visual Studio .NET there is a good chance I will drown :-). Thank you, Quecumber256
-
Dave: If I read your example right you are sending the DataSet of only those records that where modified? So your example will show the total number of records in the dataset that where modified(changed)? FYI - I'm so wet behind the ears in Visual Studio .NET there is a good chance I will drown :-). Thank you, Quecumber256
All you have to do is read the documentation on DataSet.GetChanges()[^].
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007