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:
After
Or, as viewed from the LightSwitch application, the Product prices before clicking UpdateAllProductPrices button and after clicking the button.
After

The first article was like going up the hill and the second one was like running down the hill.
I hope the team gets to read your article to get ideas to do some automation.
Dan, you’re an excellent writer and communicator. I think you’re a great candidate to write a book.
From reading your Bio, sounds like you & I started Computer Science from the same time. Yes, I started punch cards on IBM 360/370 first then moved to Univac 1108…
Ben,
Thanks for the comments!
Regarding another comment, I just updated the Part One of the post to include a zip file containing the WCF service project files and the Button_Execute code for the SearchSimpleProducts screen.
You should be able to see a link to the zip file at the bottom of the the Part One post.
Let me know how that works out for you.
Thanks!
Dan
Yes, I see the link, thanks! This way I can see the code better.
BTW, how come you blog post date says Jan 24?
Pingback: Tweets that mention How to Connect to a WCF service from LightSwitch- Part Two | Dan Moyer's Blog -- Topsy.com
Pingback: How to interface LightSwitch to a Windows Workflow Part 1 | Dan Moyer's Blog
Thank you.
Thank you Neil! I’ve gone dark a few months on this blog. Started a new job in February and have been heads down learning many new things for the job and expanding my knowledge in other areas of WCF / MEF / WPF / LightSwitch. The stack of topics I want to blog about has been growing and I want to get back to sharing what I’ve been learning real soon now. Seeing that people are continuing to come across the site, reading it, provides motivation to come out of the dark and start blogging more.