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?

1 Answer 1

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

4
  • 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, 2013 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, 2013 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, 2013 at 13:57
  • 1
    The FileHelpers error handling is not currently supported. See this similar answer.
    – shamp00
    Apr 12, 2013 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.