What is the best way to guarantee data is saved?

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I have a grid and a "save" button to persist data to a local DataSet XML
file, but am concerned that users may modify data and then attempt to exit
the application without having committed their changes.

What is the best way to handle this?
 
If they try to close the application without saving you could prompt them
with a messagebox asking them if they'd like to save.
 
Tom,

Create a boolean variable in your form, say bHasBeenSaved with the default
set to False.

When the users press the Save button, set bHasBeenSaved to True.

Then, handle the form's "Closing" event, and check if bHasBeenSaved is set
to True.

If bHasBeenSaved = False, then set the EventArgs' Cancel property to True to
cancel the form being closed. i.e.,

Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

If Not bHasBeenSaved Then

e.Cancel = True

End If

End Sub


HTH,
Brandon
 
Hi,

I assume your xml is in a dataset ds. I check to see if there are
changes in the closing event if there are then I prompt the user to save the
changes.

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If ds.HasChanges Then
If MessageBox.Show("Save changes?", "Changes Made",
MessageBoxButtons.YesNo) = DialogResult.Yes Then
ds.WriteXml("MyXmlFile.xml")
End If
End If
End Sub

Ken
 
I agree with Ken's solution, I would just add that it's impossible to
guarantee it, but you could add some pretty sophisticated exception handling
so that if a directory or file wasn't available you wrote it elsewhere. You
could similarly send it to a queue or similar structure, and if you really
need to guarantee it's written, I'd have a few different failsafe mechanisms
in place.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com
 
Back
Top