VKinfotek Inc.
  • Us
    About Us
    Contact Us
  • FAQs
  • Ready Software
    Ready ERP ASP Core Software for Azure Cloud
    Ready Web ERP Software
    Ready ASP.Net Azure Software
    Ready C# SQL Server Accounting Software
    Ready ASP.Net MVC and EF Software
First slide
Earn $100/hour in USA! Click Here

Q82. How to Update records in a table storage?

To update an entity, we use the Replace table operation. The first step is to retrieve the entity from the table and change the values to the required properties and perform the Replace operation.

  
//Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

//Create the table client
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

//Create the CloudTable object that represents the "items" table.
CloudTable table = tableClient.GetTableReference("items");

// Create a retrieve operation that takes an item entity.
TableOperation retrieveOperation = TableOperation.Retrieve<Item>("RawMaterial","R001");

// Execute the operation.
	TableResult retrievedResult = table.Execute(retrieveOperation);


We can also update an entity using Insert-or-Replace method.

Insert-or-Replace method


For the Replace operatin to be successful, we must retrieve the entity from the server. The problem with Replace operation is that sometimes we do not know whether the entity exists in the server and also the entity has been changed since it was retrieved. In such a scenario, the Replace operation will return an error and the Insert-or-Replace is useful. This operation inserts an entity if there is no entity and the entity is replaced even though update was made since we retrieved it.

  

// Retrieve the storage account from the connection string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the table client
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Create the CloudTable object that represents the "items" table
CloudTable table = tableClient.GetTableReference("items");

// Create a retrieve operation that takes a item entity
TableOperation retrieveOperation = TableOperation.Retrieve<Item>("RawMaterial", "R001");
//Execute the operation
TableResult retrievedResult = table.Execute(retrieveOperation);

// Assign the result to a Item object.
ItemEntity updateEntity = (ItemEntity)retrievedResult.Result;

if (updateEntity != null)
{
   //Change the description
   updateEntity.Description = "in nos";

   // Create the InsertOrReplace TableOperation
   TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(updateEntity);

   // Execute the operation.
   table.Execute(insertOrReplaceOperation);
   Console.WriteLine("Entity was updated.");
}




  • Prev Question
  • Next Question

See More Questions and Answers on - Azure Table Storage (cont..)

  • How to access the Azure table storage from a .NET application?
  • What is an Azure storage connection string?
  • How to configure Azure connection strings?
  • How to configure Azure connection strings for connecting to the storage emulator?
  • How to create a table in Azure storage?
  • How to create a table using a simple c# console application?
  • How to insert entities into the Azure table?
  • How do I retrieve a single entity from table storage using tableoperation?
  • How do I query azure table storage using TableQuery class?

Back to Master List of 100+ Questions and Answers

People also viewed: cover image of book develop erp software using asp core and deploy to Azure Create your own ERP Software using ASP Core>>
cover image of azure cloud book Create your own Azure SOM Software>>
cover image of asp.net book Create your own ERP Software using ASP .Net and SQL>>
cover image of foundation database programming book Create your own Accounting Software using C# >>
cover image of entity framework book Create your own SOM using Entity Framework MVC >>

  • SITEMAP
  • Terms of use
  • Testimonials

Back to top

Copyright © 2018 - All Rights Reserved - VKInfotek.com