FileHelpers - ErrorMode SaveAndContinue

  • Contact / Feedback
  • Introduction
  • Download
  • Quick Start
  • Must Read
  • Examples
    • QuickStart
      • Read Delimited File
      • Write Delimited File
      • Read Fixed File
      • Write Fixed File
      • Read or Write Record by Record
      • Autoproperties
    • Missing Values
      • Handle Missing Values with Nullable
      • Handle Missing Values With FieldNullValue
    • Attributes
      • FieldTrim
      • FieldOrder
      • FixedLengthRecord FixedMode.AllowLessChars
    • Converters
      • Enum Converter
      • Custom Converter
    • Events And Notification
      • INotifyRead Interface
      • INotifyWrite Interface
      • Before/After Read Event Handling
      • Before/After Write Event Handling
    • ErrorHandling
      • ErrorMode.ThrowException
      • ErrorMode.IgnoreAndContinue
      • ErrorMode SaveAndContinue
    • Advanced
      • Dynamic Engine Options
      • Multiple Delimiters
      • Multi Record Processing
      • Smart Format Detector
    • Sorting
      • Sort Big File with Record Class
      • Sort Big File without Record Class 1
      • Sort Big File without Record Class 2
    • MasterDetail
      • Master Detail Custom Selector
      • Master Detail Common Selector
  • Credits
  • Invite us a beer
  • Contribute
  • Source Code
  • Api Docs (Sandcastle)
  1. Examples
  2. ErrorHandling
  3. ErrorMode SaveAndContinue
Previous: ErrorMode.IgnoreAndContinue
Next: Dynamic Engine Options
Click button to edit
ErrorHandling: ErrorMode SaveAndContinue
Read the file saving bad records
Input.txt
ALFKI|Alfreds Futterkiste|Maria Anders|SalesRepresentative
ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|NotInEnum
FRANR|France restauration|Carine Schmitt|MarketingManager
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner
Customers with Enum.cs
[DelimitedRecord("|")]
public class Customer
{
    public string CustomerID;
    public string CompanyName;
    public string ContactName;
    public CustomerTitle ContactTitle;
}

public enum CustomerTitle
{
    Owner,
    SalesRepresentative,
    MarketingManager
}
RunEngine.cs
public override void Run()
{
    var engine = new DelimitedFileEngine<Customer>();

    // Switch error mode on
    engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;

    //  This fails with not in enumeration error
    var customers = engine.ReadFile("Input.txt");

    if (engine.ErrorManager.HasErrors)
        engine.ErrorManager.SaveErrors("errors.out");

    LoadErrors();
}

private void LoadErrors()
{
    // sometime later you can read it back using:
    ErrorInfo[] errors = ErrorManager.LoadErrors("errors.out");

    // This will display error from line 2 of the file.
    foreach (var err in errors) {
        Console.WriteLine();
        Console.WriteLine("Error on Line number: {0}", err.LineNumber);
        Console.WriteLine("Record causing the problem: {0}", err.RecordString);
        Console.WriteLine("Complete exception information: {0}", err.ExceptionInfo.ToString());
    }
}
Errors.out
FileHelpers - Errors Saved at jueves, 26 de noviembre de 2015 04:07:27 p.m.
LineNumber | LineString |ErrorDescription
2|"ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|NotInEnum"|In the field 'ContactTitle': Error Converting 'NotInEnum' to type: 'CustomerTitle'. The value NotInEnum is not present in the Enum.
Console

Error on Line number: 2
Record causing the problem: ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|NotInEnum
Complete exception information: System.Exception: In the field 'ContactTitle': Error Converting 'NotInEnum' to type: 'CustomerTitle'. The value NotInEnum is not present in the Enum.
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs