3

I am using File Helpers 2.9.9 and I am wondering how do I get it skip over bad records instead of it just crashing?

object[] transactions = engine.ReadStream(textReader); // will crash if one record fails.

I am also having trouble with the DateTime.I can't see why it can't convert "12/22/2011" using the formats I have set.

Error Converting '"12/22/2011"' to type: 'DateTime'.  does not match any of the given formats: 'MM/dd/yyyy', 'MM/d/yyyy', 'M/d/yyyy'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: FileHelpers.ConvertException: Error Converting '"12/22/2011"' to type: 'DateTime'.  does not match any of the given formats: 'MM/dd/yyyy', 'MM/d/yyyy', 'M/d/yyyy'
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • Could you tell us what API you are using within that? Is there a reason you can't just wrap the call in a try / catch? – JaredPar Jan 15 '12 at 00:19
  • To me it looks like it tries to do the whole stream and if all records can't pass then it fails. – chobo2 Jan 15 '12 at 00:26
  • 1
    On a related note, if anyone knows how I can get ahold of Marcos, I'd really appreciate it, since my attempts to submit patches so far haven't gotten any response :( – James Manning Jan 15 '12 at 03:41
  • @MarcosMeli has responded to my post (http://stackoverflow.com/questions/8828132/field-order-in-filehelpers) so maybe you can get hold of him through stack (http://stackoverflow.com/users/203579/marcosmeli) – chobo2 Jan 15 '12 at 07:58

2 Answers2

4

1) [EDIT] - I was wrong, you can set engine.ErrorManager.ErrorMode to SaveAndContinue - see the examples @ http://www.filehelpers.com/example_errorhandling.html

2) based on the single quotes containing a string with double-quotes, I would say the problem is that you need to provide the FieldQuoted attribute - see http://www.filehelpers.com/attributes.html

James Manning
  • 13,429
  • 2
  • 40
  • 64
  • Can you post a link to your patch? – M.Babcock Jan 15 '12 at 06:54
  • @James Manning - Ya I would love to see that patch as well. Is there any ways to tell what row failed? I am taking a user uploaded document if it fails it would be nice to tell them where it failed not just the whole document failed. – chobo2 Jan 15 '12 at 07:56
  • @M.Babcock - I was wrong, the engine already has it, see http://www.filehelpers.com/example_errorhandling.html – James Manning Jan 16 '12 at 17:21
  • @chobo2 - I was wrong, the engine already has it, see filehelpers.com/example_errorhandling.html – James Manning Jan 16 '12 at 17:21
3

You can use the BeforeReadRecord event to parse the record line and set skipThisRecord = True for any records you need to skip. For instance:

FileHelperEngine engine = new FileHelperEngine(typeof(Orders)); 
// set the event here
engine.BeforeReadRecord += new BeforeReadRecordHandler(BeforeEvent); 

Then the event itself:

private void BeforeEvent(EngineBase engine, BeforeReadRecordEventArgs e)
{
    // skip any bad lines
    if (e.RecordLine.StartsWith(" ") || e.RecordLine.StartsWith("-"))
        e.SkipThisRecord = true;
}

In the example above any record which begins with a space or a '-' will be skipped, but you can apply any logic you need. You could use e.RecordLine.Split(',') to split the current line into an array of column values and then use DateTime.TryParse() to determine if the date string is valid.

shamp00
  • 11,106
  • 4
  • 38
  • 81