Export (0) Print
Expand All

DataServiceContext.UpdateObject Method

Changes the state of the specified object in the DataServiceContext to Modified.

Namespace:  System.Data.Services.Client
Assembly:  System.Data.Services.Client (in System.Data.Services.Client.dll)

public void UpdateObject(
	Object entity
)

Parameters

entity
Type: System.Object

The tracked entity to be assigned to the Modified state.

ExceptionCondition
ArgumentNullException

When entity is null.

ArgumentException

When entity is in the Detached state.

The following example retrieves and modifies an existing object and then calls the UpdateObject method on the DataServiceContext to mark the item in the context as updated. An HTTP MERGE message is sent to the data service when SaveChanges is called. This example uses the DataServiceContext generated by the Add Service Reference tool based on the Northwind data service, which is created when you complete the ADO.NET Data Services quickstart.

string customerId = "ALFKI";

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Get a customer to modify using the supplied ID.
var customerToChange = (from customer in context.Customers
                        where customer.CustomerID == customerId
                        select customer).Single();

// Change some property values.
customerToChange.CompanyName = "Alfreds Futterkiste";
customerToChange.ContactName = "Maria Anders";
customerToChange.ContactTitle = "Sales Representative";

try
{
    // Mark the customer as updated.
    context.UpdateObject(customerToChange);

    // Send the update to the data service.
    context.SaveChanges();
}
catch (DataServiceRequestException  ex)
{
    throw new ApplicationException(
        "An error occurred when saving changes.", ex);
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2014 Microsoft