# Updating an item from a Azure Storage Table

In case you are new to the Azure Storage Tables, we've reviewed the following items this week:

Today, we'll be taking a look at updating an item through C# code into an Azure Storage Table.

# Getting Started

Open the C# Console application that we were working with yesterday (opens new window) and let's add a method to:

  • Update an item based off of the table, RowKey and PartitionKey that we pass in.

# Update an item

In our Program.cs file, we'll now add in a helper method that passes in a table, RowKey and PartitionKey and the new message that we want to use.

Special thanks to Niko12 for his comment below which caused me to rewrite this method.

static void UpdateMessage(CloudTable table, string partitionKey, string rowKey, string newMessage)
{
    Thanks entity = table.GetEntity<Thanks>(partitionKey, rowKey);
    entity.Name = newMessage;

    table.UpdateEntity(entity, ETag.All, TableUpdateMode.Replace);

}
1
2
3
4
5
6
7
8

In this example, once it performs the lookup, if it is not null, then we want to update the message with the one that we specify.

# Putting it all together.

The Main method inside of the Program.cs file, we'll call our helper method.

static void Main(string[] args)
{
    var serviceClient = new TableServiceClient(ConfigurationManager.AppSettings["StorageConnection"]);

    TableClient table = serviceClient.GetTableClient("thankfulfor");
    table.CreateIfNotExists();

    //added these lines
    UpdateMessage(table, "ThanksApp", "I am thankful for the time with my family", "I am thankful for the time with my family and friends");
    Console.ReadKey();

}
1
2
3
4
5
6
7
8
9
10
11
12