I'm trying to create azure storage table and insert records into it using c#. I have successfully created the table but it is giving me 400 Bad Request storage exception while inserting record into it.
after checking into debugger:
StorageException.RequestInformation.ExtendedErrorInformation.ErrorMessage
it is showing OutOfRangeInput error
"One of the request inputs is out of range.\nRequestId:862fdbae-6002-000c-1e7c-ef9373000000\nTime:2019-04-10T09:06:05.9840359Z"
I took help of this thread Azure table storage returns 400 Bad Request
and
- tried passing null into my entity model variables to see if any of record is out of range
- tried passing "test" in RowKey
- tried passing "1"/"test" in PartitionKey
- tried passing Guid.NewGuid().ToString() or ToAzureKeyString(Guid.NewGuid().ToString()) in RowKey
- tried passing DateTimeOffset.Now in Timestamp
still it is giving me the same error. Here is my code:
public void GetGPSFileData(Config objConfig, TraceWriter log)
{
try
{
BindData objData = new BindData();
string date = DateTime.Now.Date.ToString("ddMMyyyy");
string storageTable = "TABLE" + date + objData.REPCODE;
TableStorage tableStorage = new TableStorage(objData);
CreateTableStorage(objConfig, storageTable, tableStorage, log);
}
catch (StorageException ex)
{
log.Info($"Storage Exception while reading GPS File Data from Azure Storage: " + ex.RequestInformation.ExtendedErrorInformation.ErrorMessage + DateTime.Now);
throw ex;
}
catch (Exception ex)
{
log.Info($"Error while reading GPS File Data from Azure Storage: " + ex.Message + DateTime.Now);
throw ex;
}
}
TableStorage.cs
public class TableStorage: TableEntity
{
public string CLIENTID { get; set; }
public string REPCODE { get; set; }
public int ENTRYNO { get; set; }
public string DEVICEID { get; set; }
public double LAT { get; set; }
public double LNG { get; set; }
public DateTime DATE_TIME { get; set; }
public string PCODE { get; set; }
public string PNAME { get; set; }
public DateTime TXNDATE { get; set; }
public TableStorage(BindData objData)
{
PartitionKey = objData.CLIENTID;
RowKey = ToAzureKeyString(Guid.NewGuid().ToString());
Timestamp = DateTimeOffset.Now;
CLIENTID = objData.CLIENTID;
REPCODE = objData.REPCODE;
ENTRYNO = objData.ENTRYNO;
DEVICEID = objData.DEVICEID;
LAT = objData.LAT;
LNG = objData.LNG;
DATE_TIME = objData.DATE_TIME;
PCODE = objData.PCODE;
PNAME = objData.PNAME;
TXNDATE = objData.TXNDATE;
}
public string ToAzureKeyString(string str)
{
var sb = new StringBuilder();
foreach (var c in str
.Where(c => c != '/'
&& c != '\\'
&& c != '#'
&& c != '/'
&& c != '?'
&& !char.IsControl(c)))
sb.Append(c);
return sb.ToString();
}
}
Create and Insert into Storage Table code:
public void CreateTableStorage(Config objConfig, string tableName, TableStorage tableStorage, TraceWriter log)
{
try
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(objConfig.TABLE_STORAGE_CONN_STRING);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(tableName);
table.CreateIfNotExists();
TableOperation insert = TableOperation.Insert(tableStorage);
table.Execute(insert);
}
catch(StorageException ex)
{
log.Info($"Storage Exception while inserting record into Table Storage: " + ex.RequestInformation.ExtendedErrorInformation.ErrorMessage + DateTime.Now);
throw ex;
}
catch(Exception ex)
{
log.Info($"Error while inserting record into Table Storage: " + ex.Message + DateTime.Now);
throw ex;
}
}
TableStorage class's object contains:
What can be the issue? I'm totally new azure storage table. Please help. Thanks in advance.