0

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?

netdis
  • 321
  • 3
  • 12

1 Answers1

1

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;
    }
}

shamp00
  • 11,106
  • 4
  • 38
  • 81
  • Integer is just an example, I want to catch all invalid values (when user has selected wrong column, I have SQL Select Query based on selected columns in combobox) in ErrorManager and output errors. But I think it'd be better to populate combobox with just Date columns (to choose from) than let the user search from all columns (on Document). – netdis Apr 11 '13 at 16:01
  • The best way of doing that might be to declare all the fields as `string` then and handle any conversion in FillRecordOrder. The advantage of `string` is that it never raises an `InvalidCastException` because every object has a `ToString()`. – shamp00 Apr 11 '13 at 16:12
  • Is it impossible to catch those errors using SqlServerStorage? In flat file I can just iterate through errors (foreach ErrorInfo err in engine.ErrorManager.Errors) and display every ConvertException (when ex. field is DateTime but value presented to it was different). – netdis Apr 12 '13 at 13:57
  • 1
    The FileHelpers error handling is not currently supported. See [this similar answer](http://stackoverflow.com/a/8908808/1077279). – shamp00 Apr 12 '13 at 15:29