J
Jonathan Seidner
I saw a lot of people were having a problem with this and I found an
elegent solution to
this problem so I decided to post it.
Use the following Example:
---------------------------------------
private void BindingNavigator_OnSaveItemClick(object sender,
EventArgs e)
{
OleDbConnection Connection = new
OleDbConnection(Properties.Settings.Default.ConnectionString);
OleDbTransaction Transaction = null;
try
{
Connection.Open();
Transaction = Connection.BeginTransaction();
this.SaveClients(Connection, Transaction);
Transaction.Commit();
}
catch (System.Exception ex)
{
MessageBox.Show("An error has occured. Changes will be
rolled back" + "\n" + ex.Message);
Transaction.Rollback();
}
finally
{
Connection.Close();
}
}
private void SaveClients(OleDbConnection Connection,
OleDbTransaction Transaction)
{
try
{
if (clientsTableAdapter.Connection != Connection)
{
clientsTableAdapter.Connection = Connection;
OleDbDataAdapter Adapter =
(OleDbDataAdapter)clientsTableAdapter.GetType().GetProperty("Adapter",
BindingFlags.Instance |
BindingFlags.NonPublic).GetValue(clientsTableAdapter, null);
Adapter.UpdateCommand.Transaction = Transaction;
Adapter.DeleteCommand.Transaction = Transaction;
Adapter.InsertCommand.Transaction = Transaction;
}
this.clientsTableAdapter.Update(Properties.Settings.FastForwardDataSet);
}
catch (System.Data.DBConcurrencyException ex)
{
ClientsConcurrencyManager.ResolveConcurrencyIssue(ex.Row);
this.SaveClients(Connection, Transaction);
}
}
Actually, the only part you need is this:
OleDbDataAdapter Adapter =
(OleDbDataAdapter)clientsTableAdapter.GetType().GetProperty("Adapter",
BindingFlags.Instance |
BindingFlags.NonPublic).GetValue(clientsTableAdapter, null);
NOTE: you should also add "using reflection;" in order for this to
work.
This will give you the Adapter property of the TableAdapter and then
you can do whatever you'd like with it...
no need to write partial classes for all your tableadapters, no need to
worry about code changes done by
Visual studio typed datasets wizard, thank god for reflection!
Works perfect for me!
Jonathan
elegent solution to
this problem so I decided to post it.
Use the following Example:
---------------------------------------
private void BindingNavigator_OnSaveItemClick(object sender,
EventArgs e)
{
OleDbConnection Connection = new
OleDbConnection(Properties.Settings.Default.ConnectionString);
OleDbTransaction Transaction = null;
try
{
Connection.Open();
Transaction = Connection.BeginTransaction();
this.SaveClients(Connection, Transaction);
Transaction.Commit();
}
catch (System.Exception ex)
{
MessageBox.Show("An error has occured. Changes will be
rolled back" + "\n" + ex.Message);
Transaction.Rollback();
}
finally
{
Connection.Close();
}
}
private void SaveClients(OleDbConnection Connection,
OleDbTransaction Transaction)
{
try
{
if (clientsTableAdapter.Connection != Connection)
{
clientsTableAdapter.Connection = Connection;
OleDbDataAdapter Adapter =
(OleDbDataAdapter)clientsTableAdapter.GetType().GetProperty("Adapter",
BindingFlags.Instance |
BindingFlags.NonPublic).GetValue(clientsTableAdapter, null);
Adapter.UpdateCommand.Transaction = Transaction;
Adapter.DeleteCommand.Transaction = Transaction;
Adapter.InsertCommand.Transaction = Transaction;
}
this.clientsTableAdapter.Update(Properties.Settings.FastForwardDataSet);
}
catch (System.Data.DBConcurrencyException ex)
{
ClientsConcurrencyManager.ResolveConcurrencyIssue(ex.Row);
this.SaveClients(Connection, Transaction);
}
}
Actually, the only part you need is this:
OleDbDataAdapter Adapter =
(OleDbDataAdapter)clientsTableAdapter.GetType().GetProperty("Adapter",
BindingFlags.Instance |
BindingFlags.NonPublic).GetValue(clientsTableAdapter, null);
NOTE: you should also add "using reflection;" in order for this to
work.
This will give you the Adapter property of the TableAdapter and then
you can do whatever you'd like with it...
no need to write partial classes for all your tableadapters, no need to
worry about code changes done by
Visual studio typed datasets wizard, thank god for reflection!
Works perfect for me!
Jonathan