Reputation: 23
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.
Upvotes: 2
Views: 278
Reputation: 1217
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.
Upvotes: 1