I am trying to update two related tables, so I created the code below. However, when the program runs the information is not updated to the DB and a message saying update unsucessful appears.
Please could you help me, what am I doing wrong?
private void saveToolStripButton_Click(object sender, EventArgs e)
{
UpdateData();
}
private void UpdateData()
{
this.Validate();
this.newspaperDeliverysBindingSource1.EndEdit();
this.customersBindingSource.EndEdit();
using (System.Transactions.TransactionScope updateTransaction =
new System.Transactions.TransactionScope())
{
try
{
DeleteNewspaperDeliverys();
DeleteCustomers();
AddNewCustomers();
AddNewNewspaperDeliverys();
updateTransaction.Complete();
managementDataSet.AcceptChanges();
}
catch (TransactionAbortedException ex)
{
MessageBox.Show("Update unsuccessful");
}
}
}
private void DeleteNewspaperDeliverys()
{
managementDataSet.NewspaperDeliverysDataTable deletedChildRecords;
deletedChildRecords = (managementDataSet.NewspaperDeliverysDataTable)
managementDataSet.NewspaperDeliverys.GetChanges(DataRowState.Deleted);
// Remove all deleted orders from the newspaper deliverys table
if (deletedChildRecords != null)
{
try
{
newspaperDeliverysTableAdapter.Update(deletedChildRecords);
}
catch (Exception ex)
{
MessageBox.Show("Delete Newspaper Delivery Failed");
}
}
}
private void DeleteCustomers()
{
managementDataSet.CustomersDataTable deletedCustomers;
deletedCustomers = (managementDataSet.CustomersDataTable)
managementDataSet.Customers.GetChanges(DataRowState.Deleted);
if (deletedCustomers != null)
{
try
{
// Update the Customers table.
customersTableAdapter.Update(deletedCustomers);
MessageBox.Show("Customer permanently deleted");
}
catch (System.Exception ex)
{
MessageBox.Show("Delete Customer Failed");
}
}
}
private void AddNewCustomers()
{
managementDataSet.CustomersDataTable newCustomers;
newCustomers = (managementDataSet.CustomersDataTable)
managementDataSet.Customers.GetChanges(DataRowState.Added);
if (newCustomers != null)
{
try
{
// Update the Customers table.
customersTableAdapter.Update(newCustomers);
}
catch (Exception ex)
{
MessageBox.Show("Add New Customer Failed");
}
}
}
private void AddNewNewspaperDeliverys()
{
managementDataSet.NewspaperDeliverysDataTable newChildRecords;
newChildRecords = (managementDataSet.NewspaperDeliverysDataTable)
managementDataSet.NewspaperDeliverys.GetChanges(DataRowState.Added);
if (newChildRecords != null)
{
try
{
newspaperDeliverysTableAdapter.Update(newChildRecords);
}
catch (Exception ex)
{
MessageBox.Show("Add New Order Failed");
}
}
}