//--------------------------------------------------------------------- //<Snippet27> void UpdateDB() { NorthwindDataSet.OrdersDataTable deletedChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Deleted); NorthwindDataSet.OrdersDataTable newChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Added); NorthwindDataSet.OrdersDataTable modifiedChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Modified); try { if (deletedChildRecords != null) { ordersTableAdapter.Update(deletedChildRecords); } customersTableAdapter.Update(northwindDataSet.Customers); if (newChildRecords != null) { ordersTableAdapter.Update(newChildRecords); } if (modifiedChildRecords != null) { ordersTableAdapter.Update(modifiedChildRecords); } northwindDataSet.AcceptChanges(); } catch (Exception ex) { MessageBox.Show("An error occurred during the update process"); // Add code to handle error here. } finally { if (deletedChildRecords != null) { deletedChildRecords.Dispose(); } if (newChildRecords != null) { newChildRecords.Dispose(); } if (modifiedChildRecords != null) { modifiedChildRecords.Dispose(); } } }
//</Snippet4> private void LoadOrders(Customer currentCustomer) { NorthwindDataSet.OrdersDataTable orderData = ordersTableAdapter1.GetDataByCustomerID(currentCustomer.CustomerID); foreach (NorthwindDataSet.OrdersRow orderRow in orderData) { Order currentOrder = new Order(); currentOrder.OrderID = orderRow.OrderID; if (orderRow.IsCustomerIDNull() == false) { currentOrder.CustomerID = orderRow.CustomerID; } if (orderRow.IsEmployeeIDNull() == false) { currentOrder.EmployeeID = orderRow.EmployeeID; } if (orderRow.IsFreightNull() == false) { currentOrder.Freight = orderRow.Freight; } if (orderRow.IsOrderDateNull() == false) { currentOrder.OrderDate = orderRow.OrderDate; } if (orderRow.IsRequiredDateNull() == false) { currentOrder.RequiredDate = orderRow.RequiredDate; } if (orderRow.IsShipAddressNull() == false) { currentOrder.ShipAddress = orderRow.ShipAddress; } if (orderRow.IsShipCityNull() == false) { currentOrder.ShipCity = orderRow.ShipCity; } if (orderRow.IsShipCountryNull() == false) { currentOrder.ShipCountry = orderRow.ShipCountry; } if (orderRow.IsShipNameNull() == false) { currentOrder.ShipName = orderRow.ShipName; } if (orderRow.IsShippedDateNull() == false) { currentOrder.ShippedDate = orderRow.ShippedDate; } if (orderRow.IsShipPostalCodeNull() == false) { currentOrder.ShipPostalCode = orderRow.ShipPostalCode; } if (orderRow.IsShipRegionNull() == false) { currentOrder.ShipRegion = orderRow.ShipRegion; } if (orderRow.IsShipViaNull() == false) { currentOrder.ShipVia = orderRow.ShipVia; } currentCustomer.Orders.Add(currentOrder); } }
public virtual NorthwindDataSet.OrdersDataTable GetData() { this.Adapter.SelectCommand = this.CommandCollection[0]; NorthwindDataSet.OrdersDataTable dataTable = new NorthwindDataSet.OrdersDataTable(); this.Adapter.Fill(dataTable); return dataTable; }
//--------------------------------------------------------------------- private void customersBindingNavigatorSaveItem_Click(object sender, EventArgs e) { //<Snippet10> this.Validate(); this.ordersBindingSource.EndEdit(); this.customersBindingSource.EndEdit(); NorthwindDataSet.OrdersDataTable deletedOrders = (NorthwindDataSet.OrdersDataTable) northwindDataSet.Orders.GetChanges(DataRowState.Deleted); NorthwindDataSet.OrdersDataTable newOrders = (NorthwindDataSet.OrdersDataTable) northwindDataSet.Orders.GetChanges(DataRowState.Added); NorthwindDataSet.OrdersDataTable modifiedOrders = (NorthwindDataSet.OrdersDataTable) northwindDataSet.Orders.GetChanges(DataRowState.Modified); try { // Remove all deleted orders from the Orders table. if (deletedOrders != null) { ordersTableAdapter.Update(deletedOrders); } // Update the Customers table. customersTableAdapter.Update(northwindDataSet.Customers); // Add new orders to the Orders table. if (newOrders != null) { ordersTableAdapter.Update(newOrders); } // Update all modified Orders. if (modifiedOrders != null) { ordersTableAdapter.Update(modifiedOrders); } northwindDataSet.AcceptChanges(); } catch (System.Exception ex) { MessageBox.Show("Update failed"); } finally { if (deletedOrders != null) { deletedOrders.Dispose(); } if (newOrders != null) { newOrders.Dispose(); } if (modifiedOrders != null) { modifiedOrders.Dispose(); } } //</Snippet10> }