Thanks, Bart, that is what I was seeking.
I have a lot to learn about BindingManager, CurrencyManager, PropertyManager, and DataBinding.
In my case, the EndCurrentEdit didn't throw a Constraint Exception; most likely because I used the
defaults when Filling the DataSet and thus didn't transfer the Constraints from the Database to the
DataSet. However, the Update to the Database did throw an Constraint Exception. I trapped it as
shown below. I placed a 'Apply Changes' Button on the Form that will allow the user to post any
changes made back to the Database at will. Then, if there are a number of changes, the user can
make a few, apply them, make a few more, apply them, and so on.
QUESTION: Do you generally recommend loading all the schema into a DataSet -- or do you generally
use the very limited schema provided by default when doing a Fill?
MY CODE (THAT SEEMS TO WORK):
/*******************************************************************************************
@Closing of Form1()
*******************************************************************************************/
private void Form1_Closing( object sender, System.ComponentModel.CancelEventArgs e )
{
try
{
BindingContext[dsAuthors1, "authors"].EndCurrentEdit();
}
catch ( Exception Excpt )
{
MessageBox.Show( "Error: \n" + Excpt.Message );
}
// MessageBox -- Save changes to data (Y-N)?
// Does the user want to apply changes to the Database?
if ( dsAuthors1.HasChanges() )
{
if ( DialogResult.Yes == MessageBox.Show( Form1.ActiveForm,
"Save changes to data?",
"Exiting",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question ) )
{
applyChanges(); // Invokes oleDbDataAdapter1.Update( dsAuthors1 ) to post
// changes to the Db
}
}
}
/******************************************************************************************
@ApplyChanges() -- Sends all changes in the DataSet back to the Database via
DataAdapter Update().
*******************************************************************************************/
private void applyChanges()
{
if ( dsAuthors1.HasChanges() )
{
// Improvement: Need to get only changesto the Dataset with GetChanges() and use
// them rather than the complete DataSet
try
{
oleDbDataAdapter1.Update( dsAuthors1 );
}
catch ( Exception Excpt )
{
MessageBox.Show( "ERROR: \n" +
"Update of authors table failed. \n" +
"All changes made since the last 'Apply Changes' will be discarded.
\n\n" +
Excpt.Message );
}
try
{
dsAuthors1.AcceptChanges(); // Tell the DataSet to Update itself by applying
// all changes
}
catch ( Exception Excpt )
{
MessageBox.Show( "Error on dsAuthors1.AcceptChanges(): \n" + Excpt.Message );
}
}
}
Bart Mermuys - 16 Feb 2006 15:30 GMT
Hi,
> Thanks, Bart, that is what I was seeking.
>
[quoted text clipped - 16 lines]
> DataSet -- or do you generally
> use the very limited schema provided by default when doing a Fill?
In NET1.1 i mostly used untyped DataSet's, schema loaded by a Fill. But in
NET2.0 there is a new concept of Data Sources (note the space). A project
can have a number of Data Source's ( *typed* DataSets, custom lists or
objects).
DataAdapter.Update may throw an exception and then it won't save other
pending changes, so you could also set DataAdapter.ContinueUpdateOnError to
true and then check DataTable.GetErrors/DataTable.HasErrors afterwards.
You almost never have to call AcceptChanges yourself, in your example you
don't need to call AcceptChanges since Update will do that for each row that
was succesfully saved.
And only updating the changed rows won't provide much improvement over
updating the entire DataTable (it can even cause more problems if you use
autonumbers.) In the end both methods need to find the rows that have
changed. (The DataAdapter won't update a row that didn't changed).
HTH,
Greetings
THAT SEEMS TO WORK):
> /*******************************************************************************************
> @Closing of Form1()
[quoted text clipped - 71 lines]
> }
> }