Sample 5. Using transactions and request canceling.
- Click "Load" using datumnode/datumnode as username/password
- Edit a row in the Customer grid
- Edit rows in the Orders grid
- Select rows in the Orders grid to update
- Click "Update Rows"
- Click "Load"
private void button2_Click(object sender, RoutedEventArgs e)
{
var customer = dataGrid1.SelectedItem as Sample.Northwind.Customer.Get.Entity;
if (customer != null)
{
button2.IsEnabled = false;
button3.IsEnabled = true;
var orders = dataGrid2.SelectedItems != null && dataGrid2.SelectedItems.Count > 0 ?
dataGrid2.SelectedItems.Cast<Sample.Northwind.Customer.GetOrders.Entity>().ToList() : null;
var updateCustomer = new Sample.Northwind.Customer.Update();
updateCustomer.Params.PCustomerId = customer.CustomerID;
updateCustomer.Params.PAddress = customer.Address;
updateCustomer.Params.PCity = customer.City;
updateCustomer.Params.PCompanyName = customer.CompanyName;
updateCustomer.Params.PContactName = customer.ContactName;
updateCustomer.Params.PContactTitle = customer.ContactTitle;
updateCustomer.Params.PCountry = customer.Country;
updateCustomer.Params.PFax = customer.Fax;
updateCustomer.Params.PPhone = customer.Phone;
updateCustomer.Params.PPostalCode = customer.PostalCode;
updateCustomer.Params.PRegion = customer.Region;
updateCustomer.ExecuteCompleted += (s, args) =>
OnUpdateRequestExecuted(args, null, orders != null ? () => UpdateOrders(orders) : (Action)null);
_updateRequest = updateCustomer;
updateCustomer.BeginOpen(false);
}
}
private void OnUpdateRequestExecuted(ExecuteCompletedEventArgs e, Action opened, Action executed)
{
HandleError(_updateRequest, e);
if (_updateRequest.IsOpen && _updateRequest.Error == null)
switch (e.Behavior)
{
case RunBehavior.Open:
if (opened != null)
opened();
else
_updateRequest.BeginExecuteNonQuery();
return;
case RunBehavior.Execute:
if (executed != null)
executed();
else
_updateRequest.BeginCommit();
return;
}
CloseUpdateRequest();
}
private void UpdateOrders(IEnumerable<Sample.Northwind.Customer.GetOrders.Entity> orders)
{
var updateOrder = new Sample.Northwind.Order.Update();
updateOrder.Params.POrderID = orders.Select(o => o.OrderID);
updateOrder.Params.POrderDate = orders.Select(o => o.OrderDate);
updateOrder.Params.PRequiredDate = orders.Select(o => o.RequiredDate);
updateOrder.Params.PShippedDate = orders.Select(o => o.ShippedDate);
updateOrder.ExecuteCompleted += (s, args) => OnUpdateRequestExecuted(args, null, null);
updateOrder.SetConnectionFrom(_updateRequest);
_updateRequest = updateOrder;
updateOrder.BeginExecuteBatch();
}
This code snippet demonstrates how to cancel a request:
private void button3_Click(object sender, RoutedEventArgs e)
{
if (_updateRequest != null)
_updateRequest.Close();
}
Full source code