Getting Casting Exception while inserting the data to the Azure Table Storage Service!

krishna572 876 Reputation points
2022-04-30T14:38:18.97+00:00

LntQZH6.png

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();
        }
    }
}
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,714 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,276 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,036 Reputation points
    2022-04-30T15:57:51.353+00:00

    Use TableResult _result = await (TableResult) _table.ExecuteAsync(_operation); and make main async static async Task Main(string[] args)


0 additional answers

Sort by: Most helpful