Skip to content

Saving 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 saved.
E A .NET class of the data object to save.
entity .NET object to persist, 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 saves the object or if an error occurs. Applies  to the non-blocking method only.

Return Value

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

Example

Dictionary<string, object> contact = new Dictionary<string, object>();
contact[ "name" ] = "Jack Daniels";
contact[ "age" ] = 147;
contact[ "phone" ] = "777-777-777";
contact[ "title" ] = "Favorites";

// save object synchronously
Dictionary<string, object> savedContact;
savedContact = Backendless.Data.Of( "Contact" ).Save( contact );

// save object asynchronously
AsyncCallback<Dictionary<string, object>> callback;
callback = new AsyncCallback<Dictionary<string, object>>(
  result =>
  {
    // object has been saved
  },

  fault =>
  {
    // server reported an error
  } );

Backendless.Data.Of( "Contact" ).Save( contact, callback );

Consider the following class.

using System;

namespace com.mbaas
{
  public class Contact
  {
    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:
Contact contact = new Contact();
contact.Name = "Jack Daniels";
contact.Age = 147;
contact.Phone = "777-777-777";
contact.Title = "Favorites";

// save object synchronously
Contact savedContact = Backendless.Data.Of<Contact>().Save( contact );

// save object asynchronously
AsyncCallback<Contact> callback = new AsyncCallback<Contact>(
    result =>
    {
      // object has been saved
    },

    fault =>
    {
      // server reported an error
    } );
Backendless.Data.Of<Contact>().Save( contact, callback );

Codeless Reference

data_service_saving_object

where:

Argument                Description
table name Name of the data table where a new record must be saved.
object An object to save in the database. Object properties must match the names of the table columns. The object must not have the objectId property.
return result Optional parameter. When this box is checked, the operation returns the saved object with the objectId property.

Returns the saved object with the objectId property assigned by Backendless

Consider the following structure of the data table called employees:
data_service_example_saving_object_1

For demonstration purposes, the data table presented above has three custom columns: name, position, and phoneNumber. The objectId is a system column that contains unique identifiers of the records in the table. When a new record is saved to the table, the system assigns a unique objectId to it. The objectid is used in various operations as an access point to a specific record.

The example below saves a new object to the employees data table:

data_service_example_saving_object_2

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

data_service_example_saving_object_3

The operation described above has returned the newly saved object:

data_service_example_saving_object_4