Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check if the database must be saved when you close a C# form

Status
Not open for further replies.

polocar

Programmer
Sep 20, 2004
89
IT
Hi,
I’m writing a C# program that interfaces with a SQL Server 2005 database (I use Visual Studio 2005 Professional Edition as compiler).
I have defined the classical ADO.NET SqlDataAdapter, DataSet and DataTable objects so that, once the database has been loaded with the SqlDataAdapter Fill method, you work with the data off-line.
I would like to put a control in the Form OnClosing event handler method: if there are some changes pending, there should be a MessageBox that asks the user if he wants to save the changes; otherwise (no changes pending), the Form closes without warnings.
The only way I have thought to do that is the following:

Code:
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
   Base.OnClosing(e);

   DataRow[] adrChangedRows = dtTable.Select(“”, “”,   
                            DataViewRowState.Added | 
                  DataViewRowState.ModifiedCurrent | 
                            DataViewRowState.Deleted);

   if (drChangedRows.Length > 0)
   {
      DialogResult dlgChange = MessageBox.Show(“The database has changed; do you want to save the changes?”, “Warning”, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

      if (dlgChange == DialogResult.Yes)
         daTable.Update(drChangedRows);
      else if (dlgChange == DialogResult.No)
         dtTable.RejectChanges();   // probably unuseful, but for clearness
      else  // if (dlgChange == DialogResult.Cancel)
         e.Cancel = true;
   }
}

Do you think there is a faster (and more elegant) way to do that?

Thank you very much
 
This looks ok.. Why change it?
The one that i would personally do is to replace the ifs with a switch.
 
Why mess about selecting individual rows?
Code:
DataTable t = dtTable.GetChanges(DataViewRowState.Added +
   DataViewRowState.ModifiedCurrent +
   DataViewRowState.Deleted);

if (t != null) {
   // do stuff
}
Another alternative is to set up an event handler for the OnRowChanged event that sets a boolean dirty flag that you can inspect during closing.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Understanding what each method does will help you decide which is appropriate.

DataTable.Select(...) returns an array of DataRow references.
DataTable.GetChanges() returns a new copy of the DataTable object containing the changed rows.

(As you might have noticed, these methods return extra set of data that you may not need at all.)

If your datatable is contained in a dataset, you can also use DataSet.HasChanges(). (But I don't know exactly how it works to determine the changes. There is this one tool but I forgot the name of the disassembler tool from MSDN, it produces C# codes).

Or, just use a bool flag as stevexff suggested.

my 2cents,
[wink]
 
The tool is Lutz Roeder's Reflector for .NET. It's brilliant. And free...


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top