Use TableResult _result = await (TableResult) _table.ExecuteAsync(_operation);
and make main async static async Task Main(string[] args)
Getting Casting Exception while inserting the data to the Azure Table Storage Service!
krishna572
881
Reputation points
While adding the entity data to the table in Azure Table Storage using TableOperation class, showing me some casting exception.
Could anyone help me here!
Code:
Customer.cs
csharp
using Microsoft.WindowsAzure.Storage.Table;
namespace AzureTable
{
public class Customer : TableEntity
{
public string customername { get; set; }
public Customer(string _customername, string _city, string _customerid)
{
PartitionKey = _city;
RowKey = _customerid;
customername = _customername;
}
}
}
Program.cs
csharp
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System;
namespace AzureTable
{
class Program
{
private static string connection_string = "<Azure-Storage-Conn-String>";
private static string table_name = "Customer";
static void Main(string[] args)
{
CloudStorageAccount _account = CloudStorageAccount.Parse(connection_string);
CloudTableClient _tableClient = _account.CreateCloudTableClient();
CloudTable _table = _tableClient.GetTableReference(table_name);
//Data Creation
Customer _customer = new Customer("UserA", "Chicago", "C1");
TableOperation _operation = TableOperation.Insert(_customer);
TableResult _result = _table.ExecuteAsync(_operation); //Getting Casting Exception here as shown in the image
Console.WriteLine("Entity is added");
//_table.CreateIfNotExistsAsync();
//Console.WriteLine("Table has been creaed.");
Console.ReadKey();
}
}
}
ReadEntity
csharp
using System;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage;
using System.Threading.Tasks;
namespace AzureTable
{
class ReadEntities
{
private static string connection_string = "Azure-Storage-Account-Connection-String";
private static string table_name = "Customer";
private static string partition_key = "Chicago";
private static string row_key = "C2";
static async Task Main(string[] args)
{
CloudStorageAccount _account = CloudStorageAccount.Parse(connection_string);
CloudTableClient _table_client = _account.CreateCloudTableClient();
CloudTable _table = _table_client.GetTableReference(table_name);
TableOperation _operation = TableOperation.Retrieve<Customer>(partition_key, row_key);
//TableResult _result = await (TableResult )_table.ExecuteAsync(_operation);
var _result = await _table.ExecuteAsync(_operation);
Customer _customer = _result.Result as Customer;
Console.WriteLine($"The customer name is {_customer.customername}");
Console.WriteLine($"The customer city is {_customer.PartitionKey}");
Console.WriteLine($"The customer id is {_customer.RowKey}");
Console.ReadKey();
}
}
}
Accepted answer
-
Karen Payne MVP 35,461 Reputation points
2022-04-30T15:57:51.353+00:00