This is the final post in a four part series to demonstrate how to use the standard MSFT technologies to implement a CRUD data application in Silverlight. The steps in this post build upon those completed in part 3. 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.
This app is built on Visual Studio 2008 SP1, SQL Server 2008, Silverlight 2.0 and the Silverlight Toolkit using C#.
The finished code for part 4 is here.
1. Open the CRUDSvc.svc.cs file in the SimpleCRUDApp.Web project and add the following code below the UpdatePerson function
public void DeletePerson(Person p)
CRUDEntities ctx = new CRUDEntities();
2. Rebuild the SimpleCRUDApp.Web project and update the CRUDSvc service reference in the SimpleCRUDApp project
3. Add the following code directly after the btnSave control in Page.xaml in the SimpleCRUDApp project
<Button Content="Delete" FontSize="16" x:Name="btnDelete" Margin="5,0,0,0" />
The above XAML adds a button next to the save button to allow the user to delete a Person record.
4. Add the Click attribute to the btnDelete control in Page.xaml and select “<New Event Handler>”; the click event handler for the button control will be generated in Page.xaml.cs
5. Add the following code to the btnDelete_Click event handler in Page.xaml.cs
CRUDSvc.Person p = bDetails.DataContext as CRUDSvc.Person;
if (p != null)
bWorking.Visibility = Visibility.Visible;
CRUDSvc.CRUDSvcClient proxy = new CRUDSvc.CRUDSvcClient();
proxy.DeletePersonCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_DeletePersonCompleted);
This code retrieves the Person object from the data context of the details panel bDetails, sets the working message to visible to tell the user a asynchronous operation is in progress, defines the callback for the WCF service function and calls the WCF service function.
6. Add the following definition for the proxy_DeletePersonCompleted event handler to Page.xaml.cs after the btnDelete_Click event handler
void proxy_DeletePersonCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
bDetails.Visibility = Visibility.Collapsed;
This code reloads all the Person records to the list box and hides the details panel.
7. Run the application and select one of the Person records in the list box
8. Click the delete button. The person record will be deleted from the database
Congratulations! You now have completed the full simple CRUD application!
What have we covered?
Over the course of this four part series we have shown how to develop a VERY simple CRUD application in Silverlight 2.0, utilizing WCF, Entity Framework and SQL Server 2008 in Visual Studio SP1.
Obviously to develop a useful enterprise grade business application in Silverlight there are many other aspects to consider in architecture, logical design and implementation. The Silverlight runtime is a subset of the .NET runtime and so there are many functions that it simply doesn’t support – but I would argue that the typical real world implemented business application doesn’t use or need to use many of the functions that don’t exist in Silverlight compared to the full .NET runtime.
Silverlight still has a way to go in certain areas, but in other areas it far excels compared to many of the other client side browser technologies. Silverlight 3.0 beta is out and available and is very exciting. Also in development is .NET RIA services to support challenges of developing for multi-tier asynchronous business Silverlight and ASP.NET applications - which is very exciting. We will get to these topics in time.
In my next post we will look at another common business application development task – developing re-usable user controls with custom data related properties. This is easily done but to take advantage of the power of the data binding mechanisms in Silverlight developers need to understand what dependency properties are and how they work.