Search code examples
c#filehelpers

ErrorHandling in Filehelpers when using SqlStorage


I provided record containing column with integer type, instead of reporting errors (as documented here) got InvalidCastException in method below (for filling records in storage):

protected void FillRecordOrder(object rec, object[] fields)
{
    OrdersVerticalBar record = (OrdersVerticalBar) rec;

    record.OrderDate = (DateTime) fields[0];
}

How to handle errors using SqlStorage in Filehelpers library?


Solution

  • What are the contents of fields[0]? Are you saying it contains an integer? Then you need to convert it somehow to a DateTime. Something like:

    protected void FillRecordOrder(object rec, object[] fields)
    {
        OrdersVerticalBar record = (OrdersVerticalBar) rec;
    
        if (fields[0] == null)
          record.OrderDate = DateTime.MinValue;
        else if (fields[0] is DateTime)
          record.OrderDate = (DateTime)fields[0];
        else if (fields[0] is int)
        {
          DateTime baseDate = new DateTime(1900, 1, 1);
          DateTime newDate = baseDate.AddDays((int)fields[0]);
          record.OrderDate = newDate;
        }
    }