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
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.
The problem is coming because of the value of TXNDATE
attribute. If you look at the picture you shared, the value you're sending is 1/1/01
(i.e. DateTime.MinValue
) whereas the minimum value allowed is 1/1/1601
.
Once you pass proper value for TXNDATE
, you should not see this error.
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.
Do refer to this answer.
The possible corrected code:
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(){}//Added default constructor
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With