2

I have a table storage table from which I want to fetch some data. The insert and update queries work fine but I am having issues when I try to select some records. Below is the code I have done so far:

class TransactionEntity : TableEntity
{
    public String s{ get; set; }
    public Int32 r { get; set; }
    public String e{ get; set; }
    public String t{ get; set; }
    public String l{ get; set; }
    public TransactionEntity(String id, String s, String e, String t)
    {
        this.r= 0;
        this.s= s;
        this.RowKey = id;
        this.PartitionKey = Guid.NewGuid().ToString();
        this.e= e == null ? "" : e;
        this.t= t== null ? "" : t;
    }
}

The code for managing the Table:

class TableStorageManager
{
    public Boolean AddTransaction(TransactionEntity dto)
    {
        try
        {
            // Create the table client.
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));
            table.CreateIfNotExists();
            TableOperation op = TableOperation.Insert(dto);    
            table.Execute(op);    
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    public List<TransactionEntity> RetrieveAllFailedTransactions()
    {
        try
        {
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));         
            TableQuery<TransactionEntity> query = new TableQuery<TransactionEntity>().Where("s eq '" + ConfigurationManager.AppSettings.Get("E") + "' and r lt " + ConfigurationManager.AppSettings.Get("M") + "");
            query.Take(ConfigurationTasks.GetResultLength());  
            return table.ExecuteQuery(query).ToList();
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public Boolean UpdateTransactionStatus(TransactionEntity dto)
    {
        try
        {                    
            CloudTableClient tableClient = new CloudTableClient(new System.Uri(ConfigurationManager.AppSettings.Get("TableStorageURI")), new StorageCredentials(ConfigurationManager.AppSettings.Get("TableStorageUser"), ConfigurationManager.AppSettings.Get("TableStoragePassword")));                   
            CloudTable table = tableClient.GetTableReference(ConfigurationManager.AppSettings.Get("TableStorageName"));
            dto.ETag = "*";
            TableOperation op = TableOperation.Replace(dto);
            table.Execute(op);    
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}

Also I have changed the variable names so sorry if you people get a bit of trouble reading them.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    You say you're "having issues" - but please could you be more precise? What exactly is happening? I note that you're currently swallowing all exceptions - I would *start* by removing those try/catch blocks, so that exceptions just propagate up... or at least log the exception along the way. That way you'll get a lot more information about what's wrong, if an exception *is* being thrown. – Jon Skeet Apr 10 '19 at 09:11
  • Please edit your question and include the values for `ConfigurationManager.AppSettings.Get("E")`, `ConfigurationManager.AppSettings.Get("M")` and `ConfigurationTasks.GetResultLength()`. I believe there's an issue with values with one of these. – Gaurav Mantri Apr 10 '19 at 09:57

1 Answers1

1

I believe that there might be a default/parameter-less constructor missing from your class inheriting the TableEntity. The parameter-less constructor is very much needed to deserialize the object when recieved from the TableStorage.

So change your TableEntity code to something like:

class TransactionEntity : TableEntity
{
    public String s{ get; set; }
    public Int32 r { get; set; }
    public String e{ get; set; }
    public String t{ get; set; }
    public String l{ get; set; }
    public TransactionEntity(){//do nothing}
    public TransactionEntity(String id, String s, String e, String t)
    {
        this.r= 0;
        this.s= s;
        this.RowKey = id;
        this.PartitionKey = Guid.NewGuid().ToString();
        this.e= e == null ? "" : e;
        this.t= t== null ? "" : t;
    }
}

Although still I believe it would be great if you can share a bit more details about whatever exception you are getting on running your code. Also refer to this link for better explanation of working with TableStorage.

Tayyab
  • 1,207
  • 8
  • 29