Wednesday, July 20, 2011

Editing Entities

I am now watching the 2nd video in module 2, which describes how to edit Entities. Completed watching the video, but I will not post the timeline or notes right now. I want to do the lab, and then re-watch the video (will post the timeline then).

Moving on to the lab:
First we need to create an EditEvent page. Created the EditEvent.xaml page using the Silverlight Page template. This template also created EditEvent.xaml.cs, a C# file (which I suppose is the client code referred to in the video).

Next, I opened the Data Source window, selected Event (selected 'Details' from the drop down in front of 'Event'), and dragged the control onto the EditEvent page.

Next, I created an 'Edit' button, which we will click to edit the selected Event. The Button got created in the center of the screen under the datagrid control. I am unable to move it by dragging and dropping... I know there has to be a way to do this. Plan 2 is to try to re-position it by specifying location parameters for the button in the XAML file. I think I am unable to move the button because it is in a StackPanel. If it were in a Grid, then I would have been able to move it. However, the DataGrid control (IIRC) needs to be in a Panel container, so we may have to add a Grid to the Stack Panel and then add the Button inside the Grid.

I then added a click handler to the button, so we can Navigate to the EditEvent page. This page is given a query string which has EventID=id, where id is the id of the selected Event.

The application runs, but I realized that whichever button I click, the EditEvent page shows the same event for editing. This is because the EditEvent page still does not do anything with the EventID we passed it. We need to implement the method 'OnNavigatedTo'. In this method we will get the EventID from the url and then create a filter which will determine which record to show.

// Executes when the user navigates to this page.


protected override void OnNavigatedTo(NavigationEventArgs e)
{
string eventId = NavigationContext.QueryString["EventID"];
eventDomainDataSource.FilterDescriptors.Add(
new FilterDescriptor
{
PropertyPath = "EventID",
Operator = FilterOperator.IsEqualTo,
Value = eventId
});
}



I got the edit screens, and then added a Save button. The Save button's event handler calls submitChanges on the DomainDataService



eventDomainDataSource.SubmitChanges();



This is very interesting. I read that the client part of the domain data service, keeps track of which properties on all the Entities have changed. When we call the above method, it will save all changed Entities. So RIA services manages all the batching. I guess this batch might be run in a transaction, or perhaps we can configure it to run either way...


Got the basic infrastructure in place. I am able to select an Event, click on the Edit button, and the Edit Event page shows up. However, when I edit the Event details, and click on 'Save' the Event does not get saved.

I put a breakpoint in the Event handler code to find out what is happening, and it seems that an Exception is being thrown. (sidenote: Since the compiler did not force me to catch the exception, C# either does not have checked exceptions, or this particular Exception is a runtime Exception). This happened because I had not checked the 'enable editing' checkbox while creating the ADO Data Model. This problem can be resolved by creating placeholder methods in the domain service class for basic CRUD operations.

However, since I wanted to practice doing the project again, I deleted the project and recreated it, this time being careful to select the 'enable editing' checkbox.

Everything is working fine, however I keep running into the 'code generation' errors. Not quite sure why...


No comments:

Post a Comment