Hey all, I have encountered a serious problem while developing a new
application.

I'm using the .NET 2.0 framework for this one.

Since my users have the possibility to push the save button whenever
they want, and I want to make sure whatever they are editing gets
committed when they do, I invoke the EndEdit method on the
bindingsource prior to actually saving. After the save, I invoke the
AcceptChanges method on the dataset.

This all works as expected.

However, when the user wants to quit the current form, I have to check
the dataset for any changes that have been made to it so I can warn
them they still have unsaved data in the form. To do this, on the
FormClose, I invoke the EndEdit again (because they can exit the form
while editing) and then I use the HasChanges method on the dataset to
see if there are any changes.

When the user hasn't changed any data, this works as expected. When
the user has changed data (and hasn't saved yet) this also works.

But here's the thing: when the user has saved his data already and
then wants to quit the form, somehow, the extra invoke of the EndEdit
method on the bindingsource sets the last modified row to modified
again. So he gets the messagebox which says data has changed that he
needs to commit again, but he already has. Pushing the save button and
then trying to exit will again give the same problem.

I've checked and checked and I can not seem to find a workaround for
this problem.

Anyone with any ideas on how to fix this problem would be highly
appreciated !

Here's some mockup code to show my problem:

        private void _bindingNavigatorSaveChanges_Click(object sender,
EventArgs e)
        {
            if (EndEdit())
            {
                if (SaveDataSet(_localDataSet))
                {
                    MessageBox.Show("Save OK", "OK",
MessageBoxButtons.OK, MessageBoxIcon.Information);
                    _localDataSet.AcceptChanges();
                }
                else
                {
                    MessageBox.Show("Problem during save", "Probleem",
MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void SVDefinitionForm_FormClosing(object sender,
FormClosingEventArgs e)
        {
            if (EndEdit())
            {
                if (_localDataSet.HasChanges()) //this will trigger
false true when a save has already been performed due to the EndEdit
on the bindingsource ... somehow ... :(
                {
                    if (MessageBox.Show("Unsaved changes !\nReally
quit ?",
                            "Warning", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.No)
                    {
                        e.Cancel = true;
                    }
                }
            }
            else
            {
                if (MessageBox.Show("Quit anyway? No changes will be
commited.", "Warning", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        private Boolean EndEdit(BindingSource bindingSource)
        {
            try
            {
                bindingSource.EndEdit();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Constraint error with the data you
entered.\nError: " +
                                ex.Message + ".", "Problem",
MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

-- 
You received this message because you are subscribed to the Google
Groups "DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/dotnetdevelopment?hl=en?hl=en
or visit the group website at http://megasolutions.net

Reply via email to