views:

166

answers:

1

I've been following this tutorial on how to implement a Silverlight application. The tutorial uses the Northwind database for its examples. I have been using the code samples to implement the same functionality as the tutorial shows into my own application with my own database. The last part of the tutorial shows how to add new items and associated relationships into specific tables in the Northwind database. This particular example demonstrates this functionality on 3 linked tables (Product, Order, Order_Details) using the below code:

private void addDetail_Click(object sender, RoutedEventArgs e)
{
    // Define a URI that returns the product with the specified ID.
    Uri productUri = new Uri(svcContext.BaseUri.AbsoluteUri
        + "/Products(" + this.productId.Text + ")");

    // Begin a query operation retrieve the Product object 
    // that is required to add a link to the new Order_Detail.
    svcContext.BeginExecute<Products>(productUri,
        OnProductQueryCompleted, null);
}

private void OnProductQueryCompleted(IAsyncResult result)
{
    // Use the Dispatcher to ensure that the 
    // asynchronous call returns in the correct thread.
    Dispatcher.BeginInvoke(() =>
        {
            // Get the Product returned by the completed query.
            IEnumerable<Products> queryResult =
                svcContext.EndExecute<Products>(result);
            Products returnedProduct = queryResult.First();

            // Get the currently selected order.
            Orders currentOrder = (Orders)ordersGrid.SelectedItem;

            // Create a new Order_Details object with the supplied FK values.
            Order_Details newItem = Order_Details.CreateOrder_Details(currentOrder.OrderID,
                returnedProduct.ProductID, 0, 0, 0);

            detailsBindingCollection.Add(newItem);

            // Add the new item to the context.
            svcContext.AddToOrder_Details(newItem);

            // Add the relationship between the order and the new item.
            currentOrder.Order_Details.Add(newItem);
            svcContext.AddLink(currentOrder, "Order_Details", newItem);

            // Set the reference to the order and product from the item.
            newItem.Orders = currentOrder;
            svcContext.SetLink(newItem, "Orders", currentOrder);

            // Add the relationship between the product and the new item.
            returnedProduct.Order_Details.Add(newItem);
            svcContext.AddLink(returnedProduct, "Order_Details", newItem);

            // Set the reference to the product from the item.
            newItem.Products = returnedProduct;
            svcContext.SetLink(newItem, "Products", returnedProduct);
        }
    );
}

My Database Scripts:

CREATE TABLE InmarsatZenith.dbo.ClientJob
(JobRef nvarchar(15),
Summary text
PRIMARY KEY(JobRef))

CREATE TABLE InmarsatZenith.dbo.Job
(IntRef uniqueidentifier,
JobRef nvarchar(15),
CopyDeadline datetime,
PublicationDate datetime,
Repeat bit,
BusinessType nvarchar(25),
Sector nvarchar(30),
Lang nvarchar(15),
Format nvarchar(25),
CreativeRotation nvarchar(50),
TipinType nvarchar(25),
Status nvarchar(100)
PRIMARY KEY(IntRef))

CREATE TABLE InmarsatZenith.dbo.Detail
(IntRef uniqueidentifier,
Description nvarchar(100),
Date datetime
PRIMARY KEY(IntRef))

There is a one-to-many relationship between client job and job (1 client job can have many jobs). And a one-to-one relationship between job and detail.

The problem is that I want to implement the same asynchronous methods into my application but for a much simpler one to one relationship. So essentially I want to be able to add a new Job into my "Job" table and then add associated details into my "Job Details" table. I have had some trouble trying to convert this code sample to work with this particular method and would really appreciate some help in adapting this code to work for a one-to-one relationship.

If anyone could enlighten me on this subject I would really appreciate it.

Kind regards, thanks in advance.

A: 

Ended up followingBrad Abrams Tutorials which were far better and much more up to date.

Goober