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

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 .  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 , , and the using .

The finished code for part 4 is here.

Steps:

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

        [OperationContract]

        public void DeletePerson(Person p)

        {

            CRUDEntities ctx = new CRUDEntities();

 

            ctx.AttachTo("Person", p);

            ctx.DeleteObject(p);

            ctx.SaveChanges();

        }

 

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);

                proxy.DeletePersonAsync(p);

            }

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)

        {

            LoadPeople();

               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.

What’s next?

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.

Posted on Friday, July 3, 2009 9:23 PM Silverlight | Back to top


Comments on this post: A simple Silverlight CRUD application – part 4: doing the D in CRUD

# re: A simple Silverlight CRUD application – part 4: doing the D in CRUD
Requesting Gravatar...
Thanks for the tutorials. After looking at several rather complex tutorials, finally a clear and easy to follow tutorial.
Finannly I found out how to handle CRUD operations in Silverlight. Thank you very much!
Left by Roy on Nov 17, 2009 10:35 AM

# re: A simple Silverlight CRUD application – part 4: doing the D in CRUD
Requesting Gravatar...
Thanks man, you saved my life !!

THANKS AGAIN !!
Left by Julien on Jan 12, 2010 8:51 AM

# re: A simple Silverlight CRUD application – part 4: doing the D in CRUD
Requesting Gravatar...
Thanks You for this tutorials,

je galerais comme un dieux

It's really good.

THX MISTER

;)

<3
Left by Bioman_french on Jan 12, 2010 8:54 AM

# re: A simple Silverlight CRUD application – part 4: doing the D in CRUD
Requesting Gravatar...
Thanks for the tutorials. it is really nice... and it help me lot.
Left by Hardik on Jan 20, 2011 11:09 PM

# re: A simple Silverlight CRUD application – part 4: doing the D in CRUD
Requesting Gravatar...
I've tried other Silverlight CRUD tutorials and these were the easiest to follow. Being new to Silverlight, I was also not familiar with other components such as Expression Blend, MVVM, etc. that other tutorials also threw in and made the learning curve much steeper! Thanks :-)
Left by Chris Doan on May 04, 2011 2:50 PM

# re: A simple Silverlight CRUD application – part 4: doing the D in CRUD
Requesting Gravatar...
Good job mate.
Would like to read all your articles on silverlight!!

Thanks
Left by Karan on Apr 11, 2013 11:53 PM

Your comment:
 (will show your gravatar)


Copyright © PeterTweed | Powered by: GeeksWithBlogs.net