Updating Records - Understanding Table Storage - Part Four

Entity records are uniquely identified by a partition key and row key combination. Before updating the record, it is searched in the table using the RetrieveCustomer method by passing the partition key and row key as the parameter as we did in the previous exercise. If the entity exists, then only the update operation is done. We use TableOperation.Replace method to perform the update operation. In this demo, I am going to update the name of the customer Ramees to Mohammed Ramees. Once the update is over we will list all the records.

Step 1

Open the previous project and keep everything as it is, as we need RetriveAllCustomers method to verify the changes after the update. Bring back the method to retrieve a single record with some modification, here instead of showing the result as output we are returning the value to perform the update. So, we have to change the method from void to Customer. The modified method should look like this. Copy paste the following code to your class.

  1. public static Customer RetrieveCustomer(CloudTable table, string partitionKey, string rowKey)  
  2. {  
  3.    TableOperation tableOperation = TableOperation.Retrieve<Customer>(partitionKey, rowKey);  
  4.    TableResult tableResult = table.Execute(tableOperation);  
  5.    return tableResult.Result as Customer;  
  6. }  

Azure 

Step 2

Add the following method to perform the update operation. In the main method itself and we will give the new value for the update and will pass to this method.
  1. static void UpdateCustomer(CloudTable table, Customer customer)  
  2. {  
  3.    TableOperation update = TableOperation.Replace(customer);  
  4.    table.Execute(update);  
  5. }   
Azure 
 
Step 3

Add the following code to your Main method after the code for getting table reference before the RetriveAllCustomers Method. Here it will get the return value from RetrieveCustomer method and do the necessary update operation.
  1. Customer customerEntity = RetrieveCustomer(table, "registered""[email protected]");  
  2. if(customerEntity != null)  
  3.    {  
  4.       Console.WriteLine("Enter the new name for the customer");  
  5.       string newName = Console.ReadLine();  
  6.       customerEntity.CustomerName = newName;  
  7.    }  
  8. UpdateCustomer(table, customerEntity);  

Azure

 

The complete class should look like this,
  1. class Program  
  2. {  
  3.    static void Main(string[] args)  
  4.    {  
  5.       CloudStorageAccount storageAccount = CloudStorageAccount.Parse(  
  6.       CloudConfigurationManager.GetSetting("StorageConnection"));  
  7.       CloudTableClient tableClient = storageAccount.CreateCloudTableClient();  
  8.       CloudTable table = tableClient.GetTableReference("customers");  
  9.       Customer customerEntity = RetrieveCustomer(table, "registered""[email protected]");  
  10.    if(customerEntity != null)  
  11.       {  
  12.          Console.WriteLine("Enter the new name for the customer");  
  13.          string newName = Console.ReadLine();  
  14.          customerEntity.CustomerName = newName;  
  15.       }  
  16.       UpdateCustomer(table, customerEntity);  
  17.       RetrieveAllCustomers(table);  
  18.       Console.ReadKey();  
  19.       }  
  20.       public static Customer RetrieveCustomer(CloudTable table, string partitionKey, string rowKey)  
  21.       {  
  22.          TableOperation tableOperation = TableOperation.Retrieve<Customer>(partitionKey, rowKey);  
  23.          TableResult tableResult = table.Execute(tableOperation);  
  24.          return tableResult.Result as Customer;  
  25.       }  
  26.    static void UpdateCustomer(CloudTable table, Customer customer)  
  27.    {  
  28.       TableOperation update = TableOperation.Replace(customer);  
  29.       table.Execute(update);  
  30.    }  
  31.    static void RetrieveAllCustomers(CloudTable table)  
  32.    {  
  33.       TableQuery<Customer> query = new TableQuery<Customer>()  
  34.       .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "registered"));  
  35.    foreach (Customer customer in table.ExecuteQuery(query))  
  36.    {  
  37.       Console.WriteLine(customer.CustomerName + " " + customer.CustomerEmail);  
  38.    }  
  39.    }  
  40. }   
Step 4

Run your application and you will be prompted to give the new name for the specified record. Giving the new name will update the record and will list all the records with the updated value.
 
Azure


Similar Articles