Skip to main content

Updating an Existing Object

Here is a sample of .NET code that does the following:

  1. Uses ExistsId to verify that a Provider.Contact instance with object ID “1” exists in the database.

  2. Opens the Provider.Contact instance.

  3. Updates the Name property of the instance.

  4. Saves the updated instance to the database using Save.

  5. Outputs an error message to the console if the Save operation failed. If the IsOK property of the CacheStatus instance returned by Save is false, the operation failed.

    bool? exists = Provider.Contact.ExistsId(cnCache,"1");
    if (exists.HasValue && exists.Value)
       Provider.Contact contact = Provider.Contact.OpenId(cnCache, "1");
       contact.Name = "Smith,John";
       CacheStatus status = contact.Save();
       if (!status.IsOK)
          Console.WriteLine("Error Updating Contact");
catch (CacheException e){}

In this example, cnCache represents an open CacheConnection instance.

ExistsId returns a bool? nullable type. The value can be true, false, or null. Retrieve the bool value either by casting or by the Value property. However, if the value is null both of these operations will throw exceptions. Before accessing the bool value, verify that the value is not null using HasValue.


For more information on Nullable types in .NET, read Using Nullable Types (C# Programming Guide)Opens in a new tab in the C# Programmer's Reference.

FeedbackOpens in a new tab