Question on HasChanges function in VB.NET dataset

LucyRRD
LucyRRD used Ask the Experts™
on
Is there any VB.NET function to check whether any table within the dataset has been changed?
Note: I can not use the .HasChanges function under the dataset to distinguish which specific table has been updated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
check this property:
DataTable1.GetChanges().Rows.Count

If it's greater than 0 then there are changes.

Author

Commented:
Omego2K,
This works when there are changes to the table, however it blows when there is no change. It looks like when there is no change, the DataTable1.GetChanges().Rows.Count is null instead of zero. How can I check if the DataTable1.GetChanges().Rows.Count is null before calling the method? Please advise. Thanks!

Commented:
try this
if(DataTable1.GetChanges() == null)
{
//there are no changes
}
else
{
//there are changes
}
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
if(DataTable1.GetChanges() == null) does not work, as null is not supported.
Most Valuable Expert 2012
Top Expert 2008
Commented:
For VB.NET:

Dim changes As DataTable = DataSet1.Tables("Table1").GetChanges()

If changes IsNot Nothing Then
   
End If

Commented:
what do you mean null is not supported? show your code please.
x77

Commented:
Is fast verify all records.
DataSet.HasChanges - Do It. for each Table.

Use Events for detect changes.



  DataRowState RowStates = 
    DataRowState.Modified | DataRowState.Deleted | DataRowState.Added;
  bool changed = false; 
  foreach (DataRow row in MyTable.Rows) 
    if ((row.RowState & RowStates) != 0) {changed = true; break;} 

// Detecting changes...
 MyTable.RowChanged +=new DataRowChangeEventHandler(MyTable_RowChanged);

Open in new window

Most Valuable Expert 2012
Top Expert 2008

Commented:
"Question on HasChanges function in VB.NET dataset"
That code, and null are C#, this is a VB.NET question...

Author

Commented:
TheLearnedOne and Omego2K,

Thank you both for your help. The codes below work now!

Dim changes As DataTable = DataSet1.Tables("Table1").GetChanges()
If changes IsNot Nothing Then
   
End If

Commented:
ah, so you were using VB.NET
x77

Commented:
I'am sorry, I program Vb Net but I think This post is C#
For me, Vb is more easy to write.
x77

Commented:

Private MyTable As DataTable
 
....
   Dim RowStates As DataRowState = _
   DataRowState.Modified And DataRowState.Deleted And DataRowState.Added
   Dim changed = False
  For Each row As DataRow In MyTable.Rows
    If (row.RowState And RowStates) <> 0 Then changed = True : Exit For
  Next
 
 'Detecting changes...
  AddHandler MyTable.RowChanged, AddressOf MyTable_RowChanged

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial