Responding to a FormClosing event & has a dataset changed

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

I have a dateset whose contents can be changed by the user. Usually the user would click the save button when they are finished making the changes. However, if they forget to click on the save button, I want to simply offer them the opportunity to do so when the FormClosing event is fired. Here is my code so far:
Code:
    Private Sub frmGeneralQueries_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Select Case mintReport
            Case frmMain.mcTotalByOrganisation
                If Not dsPRTRDisplay.Tables("TotalByOrganisation").GetChanges.Rows.Count = 0 Then
                    If MessageBox.Show("Your changes have not been saved.  Do you want to Save them?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
                        AllowSaveChanges()
                    End If
                End If
        End Select
    End Sub

The above code works in the event that there was a change that the user had not already save, however, I am getting problems if no changes exist. I know that I could use dataset.haschanges, however, there is no guarantee that the dataset in question would only have one table in it.

Error message is: NullReferenceException - Object Reference not set to an instance of an object.

Any suggestions?

Mike55.
 
Last edited:
Try
Visual Basic:
 If Not dsPRTRDisplay.Tables("TotalByOrganisation").GetChanges Is Nothing
instead and see if that works.

IIRC GetChanges will return nothing if there are no modified records.
 
Back
Top