question

NFSCoder-9821 avatar image
0 Votes"
NFSCoder-9821 asked NFSCoder-9821 edited

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

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();
        }
    }
}

dotnet-csharpazure-storage-accounts
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered NFSCoder-9821 edited

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

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@karenpayneoregon , Getting the same error kMzlN27.png


0 Votes 0 ·

I can't see the error message so try and then hover over result, what does this tell you the type is?

 var _result = await _table.ExecuteAsync(_operation);
0 Votes 0 ·

@Sumarigo-MSFT Could you please help me to resolve this issue!

0 Votes 0 ·

Did you do what I asked, determine what _result is when used with var?

0 Votes 0 ·
Show more comments