object.save() problem

object.save() problem

Old forum URL: forums.lhotka.net/forums/t/2917.aspx


MTmace posted on Monday, May 21, 2007

I have two scenarios where I need to save an object, when the form closes and when another object has been selected via a list box.

When another object has been selected via the list box I am executing the following code.

if (this.CurrentBatchClass != null && this.CurrentBatchClass.IsDirty)
{
   DialogResult result;

   result = MessageBox.Show(string.Format("Do you want to save changes", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result == DialogResult.Yes)
   
this.CurrentBatchClass.Save();
}

This example works fine. The same code is getting excuted when on the form's closing event.
But on the form closing event I get an error when the base.save() method is called. the error says the object is still being edited and can not be saved.

How can I avoid this issue?

MTmace

xal replied on Monday, May 21, 2007

You probably have a binding source, right?
In that case you need to call the bindingsource's EndEdit method before doing save.
What's "CurrentBatchClass" ? a property? a field? when do you set it's value?

Anyway, your problem is definitely related to n-level undo and the use of begin/end edit. Make sure you get that pattern right. There are plenty of threads on this subject.

Andrés

MTmace replied on Monday, May 21, 2007

it is an object and I found the solution on another thread. Executing apply edit before the save solved my problem.

MTmace

Copyright (c) Marimer LLC