Update changes to databse

What is the best way to manage changes to the node (by drag and drop, double click, etc) and update the same to a database.
Which events should we look to capture?
We are using RIA service in our example to bind data to the database.

Basically you want to treat each GoXam transaction as a database transaction.

The easiest way to do that is to implement a DiagramModel.Changed event handler that looks for the ModelChange.CommittedTransaction case. Here’s where you can start your database transaction.

In that event handler you can see the whole list of model changes for that transaction. That’s an UndoManager.CompoundEdit. You can iterate over all of its Edits to look at each ModelChangedEventArgs. Each describes the kind of change it was, along with relevant state, such as old and/or new values.

Some of the changes you won’t care about. For those that you do care about, you can update your database appropriately.

Note that for transactions such as “MoveSelection”, there may be a lot of “Location” property change events. You probably just care about the last location for each of the moved nodes.

The UpdateDemo sample demonstrates the notification system and actually implements a model Changed event handler similar to what you want. However it just updates a TreeView so that it can display all of the changes in the UpdateDemo user interface. You’ll want to do a database update instead. Of course such an update is very application-specific.

The UpdateDemo sample also demonstrates how to handle Undo and Redo events.

I will just rephrase my query, we are working on a demo similar to the orgchartbus demo. In this example we see that the parent id of the node is changed when we drag and drop it to another node (as displayed under label ‘Boss’). We are trying a similar transaction to the database directly, however we are unable to capture this event.
note - in case this requires us to make a change to the source code then just let us know as we have the same.

Ah, good – specific questions make it possible to give specific answers.

In the Changed event handler, when the ModelChangedEventArgs.Change == ModelChange.CommittedTransaction, the ModelChangedEventArgs.OldValue will be an UndoManager.CompoundEdit.
Its .Edits property will contain a ModelChangedEventArgs for the “tree parent id” change.

That ModelChangedEventArgs (which inherits from PropertyChangedEventArgs) should have the following properties:
Change = ModelChange.Property
PropertyName = “ParentKey”
Data = the tree-child node data being modfied
OldValue = the old boss id
NewValue = the new boss id

The data types of Data and OldValue and NewValue depend on the definitions of the model data that you are using. In the case of the OrgChartBus sample, the node data type is Employee (inheriting from TreeModelNodeData), and the values are integers because the Employee.Key property is of integer type.

Thanks for the last suggestions, it was helpful!

However we are facing an error with the script -‘Unable to cast object of type’.

I have put the code below (the error part is highlighted)-

Imports System.Windows.Controls
Imports System.Windows.Navigation
Imports Northwoods.GoXam.Model


‘’’ Home page for the application.

Partial Public Class Home
Inherits Page

''' <summary>
''' Creates a new <see cref="Home"/> instance.
''' </summary>
Public Sub New()
    Dim model = New TreeModel(Of Chart, Integer)()

    model.ChildNodesPath = ""
    ' don't use each node data's collection of children
    ' initialize it from data in an XML file that is an embedded resource

    model.Modifiable = True
    ' allow user modification
    model.HasUndoManager = True
    ' support undo/redo
    Diagram1.Model = model

    AddHandler model.Changed, AddressOf model_Changed

    Me.Title = ApplicationStrings.HomePageTitle
End Sub

''' <summary>
''' Executes when the user navigates to this page.
''' </summary>
Protected Overloads Overrides Sub OnNavigatedTo(ByVal e As NavigationEventArgs)
End Sub

Private Sub Org_viewDomainDataSource_LoadedData(ByVal sender As System.Object, ByVal e As System.Windows.Controls.LoadedDataEventArgs) Handles Org_viewDomainDataSource.LoadedData
   <b> Diagram1.NodesSource = Org_viewDomainDataSource.Data</b>

    If e.HasError Then
        System.Windows.MessageBox.Show(e.Error.ToString, "Load Error", System.Windows.MessageBoxButton.OK)
    End If

End Sub

Private Sub model_Changed(ByVal sender As Object, ByVal e As ModelChangedEventArgs)
    'MessageBox.Show("hello ... ): ")
    Dim i As Integer

    If e.Change = ModelChange.CommittedTransaction Then
        i = e.OldValue
        i = e.NewValue
    End If

End Sub
Private Sub Node_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs)

End Sub

End Class
Public Class Chart
Inherits TreeModelNodeData(Of Integer)

End Class

Could you please tell where we might be wrong?

At least for now, I suggest you use a Northwoods.GoXam.Model.UniversalTreeModel, rather than a node-data-type-specific generic TreeModel.

That means it’s a little slower and a lot less type-safe, but that probably doesn’t matter much overall.