How to: Save Data

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Pending changes are committed to a data source when the user presses the Save button in a screen. However, you can also commit pending changes by adding code that calls the SaveChanges method of a data source. You must add this code if you want to accomplish either of the following tasks:

  • Commit changes that you make to data that is located in other data sources.

  • Override the Save event of a screen.

The files in which you write custom code have a primary data source. If add custom code that modifies data from other data sources, you must commit those changes by calling the SaveChanges method of that data source.

The following example shows code then runs when a user creates an Order in a screen and then clicks the Save button. The code updates a field in the Products entity by using a field in the Order Details entity. Because the Products entity is located in another data source, this code calls SaveChanges method of that data source to commit the changes.

Public Class ApplicationDataService
         Private Sub Order_Inserting(ByVal entity As Order)
            For Each detail In entity.Order_Detail
                detail.Product.UnitsInStock = detail.Product.UnitsInStock - detail.Quantity
            Next

            Me.DataWorkspace.ProductDataSource.SaveChanges()
        End Sub
End Class

You can replace the behavior of the Save button on a screen by overriding the Save event. Because you are replacing the behavior of the Save button, your code must call the SaveChanges method when you want to commit pending changes.

The following example overrides the Saving event of a customer screen. The author of this code overrides the Saving event to catch and handle a specific exception that might be thrown if the save operation fails.

Public Class CustomerScreen

         Private Sub CustomerScreen_Saving(ByRef handled As Boolean)

            Try
                Me.DataWorkspace.ApplicationData.SaveChanges()

            Catch ex As DataServiceOperationException

                If ex.ErrorInfo = "DTSException" Then
                    Me.ShowMessageBox(ex.Message)
                End If


            End Try

            handled = True
        End Sub
End Class

To learn about exceptions that you can catch and handle when saving data, see How to: Design a Query by Using the Query Designer.

To learn more about handling screen events such as the Saving event, see How to: Handle Screen Events.

To learn more about working with pending change sets in code, see How to: Handle Screen Events.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft