In part one of this topic, I discussed how to implement a WCF service and create a proxy to the WCF service which you can use in a LightSwitch application.
In this post, I’ll show you how to use that proxy and make updates to the product table within LightSwitch.
In my exploration to learn about LightSwitch, I’ve been using the AdventureWorks Light database.
For this example, I create a new table called SimpleProduct based from the the SalesLT.Product table.
The schema for the SimpleProduct table is:
I used the following script to populate the SimpleProduct table:
I’ll assume that you already know how to create a LightSwitch application, connect to an external database, connect to the SimpleProduct table, and create a search and details screen from the SimpleProduct entity.
In my LightSwitch project I created two screens and called them SearchSimpleProduct and SimpleProductDetail.
Show the SearchSimpleProduct screen in the designer.
In the Screen Command Bar, add a new button and call it UpdateAllProductPrices:
Right mouse click UpdateAllProductPrices and click Edit Execute Code in the context menu:
In Solution Explorer, change from Logical View to File View:
Next, navigate to the Client node in the solution explorer and add a reference to the Product.Wcf.LSProductProxy assembly built in part one of this topic.
In the code file for the button execute, add the following using statement:
Next, add the following code to Button_Execute()
In the code example above, lines 14 creates an instance to the proxy which handles the asynchronous calls to the WCF service. Line 15 requests the collection of product price updates.
Recall from part one of this topic, the product price data is contained in a ProductPriceInfo object and GetAllProductPriceUpdate returns a collection of ProductPriceInfo objects.
Also recall, the WCF service implementation is hard coded, for demo purposes, to return three ProductPriceInfo items, for products FR-R92B-58, HL-U509, and SO-B909-M
Line 17 – 15 iterate through the collection of returned ProductPriceInfo objects and updates the Product table.
Line 18 queries the SimpleProduct entity using the ProductNumber.
If the product is found, the returned product entity’s ListPrice and StandardCost are updated with the data contained in the ProductPriceInfo item. Because the ProductPriceInfo item’s data is of type double, and the entity uses a type decimal (money), the code performs a conversion to a decimal type.
(I intentionally defined the data from the service as a double instead of a decimal, in the spirit of using a legacy WCF service in which you may not have control over the data types the service gives you. This demonstrates a simple case where you need to do a data conversion in the code before saving the data in the entity.)
Line 23 writes the updated product entity to the database.
Before running the update, the StandardCost and ListPrice are shown here from SQL Management Studio:
Or, as viewed from the LightSwitch application, the Product prices before clicking UpdateAllProductPrices button and after clicking the button.