Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

February 5, 2011

Updating Data in a Silverlight Application: EF

Filed under: ASP.NET RIA Services,C#,Silverlight,VB.NET @ 4:16 pm

This prior post covered the details of setting up a database, defining an entity model using Entity Framework, building WCF RIA Services to access that entity model, and hooking it all up to a Silverlight DataGrid. That is all well and good. But most line of business (LOB) applications also need to update data. That’s the purpose of this post.

NOTE: This post is part of a series that starts with this prior post. The example in this post uses the application that is built as part of that series.

NOTE: This post assumes you already have the database, entity model, and WCF RIA Service classes in place. If you need information on how to perform these prerequisite steps, see this prior post.

The first step in adding update functionality is to modify the UI. Add a Save button to a page containing a DataGrid.

The xaml for the Save button looks like this:

<Button x:Name="SaveButton"
        Grid.Column="1"
        Margin="10,10,0,10"
        Content="Save"
        HorizontalAlignment="Right"
        Width="60"
        Click="SaveButton_Click" />

Then add the following to the code behind:

In C#:

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
    studentDataGrid.CommitEdit();
    studentDomainDataSource.SubmitChanges();
}

In VB:

Private Sub SaveButton_Click(ByVal sender As System.Object,
                   ByVal e As System.Windows.RoutedEventArgs) _
                   Handles SaveButton.Click
    studentDataGrid.CommitEdit()
    StudentDomainDataSource.SubmitChanges()
End Sub

This code first completes any current edit operation on the grid. Then it uses the DomainDataSource that was defined in the xaml to submit the changes to the database.

When you run the application, the result will look something like this:

image

Edit the data in the grid and click Save to save the changes.

If you are happy with using a DomainDataSource and with saving the data in the code behind, you are done. If you want to handle the data retrieval without a DomainDataSource and follow a more MVVM approach, you still have some work to do. But that is another post.

Click here to download the associated source code.

Enjoy!

EDIT 2/8/2011: Added the link to the source code.

1 Comment

  1.   Darth Vodka — April 15, 2013 @ 9:16 am    Reply

    >If you want to handle the data retrieval without a DomainDataSource…

    I’ve been banging my head trying to do this all week 🙁

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2019 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs