Visual Basic: Dataset.HasChanges() always false

Platform:Visual Basic
Task:Dataset.HasChanges() always false
Discussion:
The Dataset object has a HasChanges method that indicates if there are changes that need to be cascaded back to the database. Sometimes HasChanges is always false, even when you know it should be true. This is most likely because some part of your code called the DataTable's AcceptChanges method. AcceptChanges resets the dataset's HasChanges event to false. Don't accept changes until you are ready to save the dataset. To trigger a change in the datasets HasChanges method you should call the datatable's EndEdit method.
Example:
Dim dsNewRow As DataRow 'make a data row
dsNewRow = SheepMonitoringDataSet.tblSurveyFiles.NewRow 'add a row to the dataset's table
dsNewRow.Item("SurveyID") = Val(SurveyIDTextBox.Text)
dsNewRow.Item("Filename") = My.Computer.FileSystem.GetFileInfo(FileToArchive).Name.Trim
SheepMonitoringDataSet.tblSurveyFiles.Rows.Add(dsNewRow)
'SheepMonitoringDataSet.tblSurveyFiles.AcceptChanges() 'don't do this
TblSurveyFilesBindingSource.EndEdit() 'do this.  SheepMonitoringDatase.HasChanges is now true
Back to Index