Skip to content

Updating Single Object

Blocking Method

Dictionary<string, object> result; 

result = Backendless.Data.Of( "TABLE-NAME" ).Save( Dictionary<string, object> );
public E Backendless.Data.Of<E>().Save( E entity );

Non-Blocking API

void Backendless.Data.Of( "TABLE-NAME" ).Save( Dictionary<string, object> entity, 
                                               AsyncCallback<Dictionary<string, object>> responder )
public void Backendless.Data.Of<E>().Save( E entity, AsyncCallback<E> responder )

where:

Argument                Description
TABLE-NAME Name of the table where the object represented by System.Collections.Generic.Dictionary will be updated.
E A .NET class of the data object to update.
entity .NET object to update, must be of type E or System.Collections.Generic.Dictionary (depending on the method used).
responder a responder object which will receive a callback when the method successfully updates the object or if an error occurs. Applies  to the non-blocking method only.

Return Value

The blocking method returns the updated object. The non-blocking call receives the return value through a callback executed on the AsyncCallback object.

Example

Blocking API

``` csharp
static void UpdateContact()
{
  // create new contact object first. Then we will update it.
  Dictionary<string, object> contact = new Dictionary<string, object>();
  contact.Add( "Name", "Jack Daniels" );
  contact.Add( "Age", 147 );
  contact.Add( "Phone", "777-777-777" );
  contact.Add( "Title", "Favorites" );

  Dictionary<string, object> savedContact = Backendless.Persistence.Of( "Contact" ).Save( contact );

  // now update the saved object
  savedContact[ "Title" ] = "Most favorite";
  Backendless.Persistence.Of( "Contact" ).Save( savedContact );
}
```

Non-blocking API

``` csharp
static void UpdateContactAsync()
{
  // create new contact object first. Then we will update it.
  Dictionary<string, object> contact = new Dictionary<string, object>();
  contact.Add( "Name", "Jack Daniels" );
  contact.Add( "Age", 147 );
  contact.Add( "Phone", "777-777-777" );
  contact.Add( "Title", "Favorites" );

  AsyncCallback<Dictionary<string, object>> updateObjectCallback;
  updateObjectCallback = new AsyncCallback<Dictionary<string, object>>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been updated" );
    },
    error =>
    {
    }
  );

  AsyncCallback<Dictionary<string, object>> saveObjectCallback;
  saveObjectCallback = new AsyncCallback<Dictionary<string, object>>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been created, now make an API call to update it" );
      // now update the saved object
      savedContact[ "Title" ] = "Most favorite";
      Backendless.Persistence.Of( "Contact" ).Save( savedContact, updateObjectCallback );
    },
    error =>
    {
    }
  );

  Backendless.Persistence.Of( "Contact" ).Save( contact, saveObjectCallback );
}
```

Consider the following class:

public class Contact
  {
    //use the Weborb.Service namespace to import the annotation
    [SetClientClassMemberName( "objectId" )]   
    public String ObjectId { get;set; }
    public String Name { get; set; }
    public int Age { get; set; }
    public String Phone { get; set; }
    public String Title { get; set; }
  }
The following code saves a new instance of the Contact class and subsequently updates it:

Blocking API

``` csharp
static void UpdateContactUsingClass()
{
  // create new contact object first. Then we will update it.
  Contact contact = new Contact();
  contact.Name = "Jack Daniels";
  contact.Age = 147;
  contact.Phone = "777-777-777";
  contact.Title = "Favorites";

  Contact savedContact = Backendless.Persistence.Of<Contact>().Save( contact );

  // now update the saved object
  savedContact.Title = "Most favorite";
  Backendless.Persistence.Of<Contact>().Save( savedContact );
}
```

Non-blocking API

``` csharp
static void UpdateContactAsyncUsingClass()
{
  // create new contact object first. Then we will update it.
  Contact contact = new Contact();
  contact.Name = "Jack Daniels";
  contact.Age = 147;
  contact.Phone = "777-777-777";
  contact.Title = "Favorites";

  AsyncCallback<Contact> updateObjectCallback = new AsyncCallback<Contact>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been updated" );
    },
    error =>
    {
    }
  );

  AsyncCallback<Contact> saveObjectCallback = new AsyncCallback<Contact>(
    savedContact =>
    {
      System.Console.WriteLine( "object has been created, now make an API call to update it" );
      // now update the saved object
      savedContact.Title = "Most favorite";
      Backendless.Persistence.Of<Contact>().Save( savedContact, updateObjectCallback );
    },
    error =>
    {
    }
  );

  Backendless.Persistence.Of<Contact>().Save( contact, saveObjectCallback );
}
```

Codeless Reference

data_service_saving_object

where:

Argument                Description
table name Name of the data table where a record must be updated.
object An object with properties that match the names of the table columns, these properties must contain new values for update operation. The objectId property and the corresponding value must be also included in the object.
return result Optional parameter. When this box is checked, the operation returns the updated object.

Returns the updated object.

Consider the following record stored in the employees data table:
data_service_example_update_2

The example below uses the objectId: "18AE9147-8016-412C-8E55-83B3188E153F" to find the record in the data table and update the value in the contactType column from "Personal" to "Work". To update more values in one query, specify the required number properties/column in the object and the new values.

data_service_example_update_1

The result of this operation will look as shown below after the Codeless logic runs:

data_service_example_update_3

Moreover, the operation described above has returned the updated object:

data_service_example_update_4