Geeks With Blogs
Peter Tweed Exploring and explaining the mysteries of .NET

This is the third post in a series to demonstrate how to use the standard MSFT technologies to implement a CRUD data application in .  The steps in this post build upon those completed in part 2.  The goal of this series is to show those of you who are relatively new to Silverlight how analogous building such an app in Silverlight is to say building it in ASP.NET, Win Forms or WPF.

The finished code for part 3 is here.


1.       Open the CRUDSvc.svc.cs file in the SimpleCRUDApp.Web project and add the following code below the AddPerson function


        public void UpdatePerson(Person p)


            EntityKey key;

            object originalItem;


            using (CRUDEntities ctx = new CRUDEntities())




                    key = ctx.CreateEntityKey("Person", p);


                    if (ctx.TryGetObjectByKey(key, out originalItem))



                            key.EntitySetName, p);





                catch (InvalidOperationException ex)






2.       Add the following using statement above the namespace declaration at the top of the CRUDSvc.svc.cs file

using System.Data;


3.       Rebuild the SimpleCRUDApp.Web project and update the CRUDSvc service reference in the SimpleCRUDApp project

4.       Add the SelectionChanged attribute to the lstPersons list box control in Page.xaml and select “<New Event Handler>”; the selection changed event handler for the list box will be generated in Page.xaml.cs

5.       Add the following code to the lstPersons_SelectionChanged event handler in Page.xaml.cs

            CRUDSvc.Person p = lstPersons.SelectedItem as CRUDSvc.Person;


            if (p != null)


                bDetails.DataContext = p;

                bDetails.Visibility = Visibility.Visible;


This code retrieves the selected Person entity object from the selected item in the list box, sets it to the data context of the details panel bDetails and displays the details panel.

6.       Replace the UpdatePerson function and the proxy_AddPersonCompleted event handler with the following code

        private void UpdatePerson(CRUDSvc.Person p)


            bWorking.Visibility = Visibility.Visible;

            CRUDSvc.CRUDSvcClient proxy = new CRUDSvc.CRUDSvcClient();


            if (p.PersonID == 0)


                proxy.AddPersonCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_AddUpdatePersonCompleted);





                proxy.UpdatePersonCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_AddUpdatePersonCompleted);





        void proxy_AddUpdatePersonCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)



            bDetails.Visibility = Visibility.Collapsed;



Notice that we have associated the update of the Person record also with the processing of the save button being clicked.  We use a simple test of the PersonID property of the Person entity to determine if this is a new record that is being added or whether it is an existing record.  The PersonID column in the Person table is the primary key that is an identity column with seed = 1 and increment  = 1, so any record committed to the database table is assigned a PersonID of greater than zero.  Once we know whether to add or update the Person object, we call the appropriate WCF service function.  Note that because in both cases in this simple example I would reload all the person objects and hide the details panel, we can use the same callback function for both.

7.       Run the application and select one of the entries in the list of people to display the Person details panel

8.       Update some piece of information about the person (I added a 1 to the end of the last name of Joe Bloggs).  Click the Save button and after the processing has occurred you’ll see the updated Person record is displayed in the list of person records

Congratulations!  You now have the CRU in the CRUD application!

What have we covered?

We have added the ability to update existing Person records in our database from our Silverlight client.

We have shown how to add the update function to the WCF service, rebuild the service and update the service reference in the Silverlight client.

We have shown that the pattern of calling the added WCF service function is again the same as the previous ones we implemented in part 1 and part 2  – i.e. asynchronous with a callback event handler.  Please note: as in Part 2 I recommend a more elegant mechanism than creating a service proxy each time you want to call a WCF service.

We have seen how to hook up the selection changed event of a list box in the XAML file to the code behind event handler.

What’s next?

In the next post we will add the ability to delete our Person records in our CRUD Silverlight application.

Posted on Friday, July 3, 2009 12:16 AM Silverlight | Back to top

Comments on this post: A simple Silverlight CRUD application – part 3: updating the U in CRUd

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © PeterTweed | Powered by: