How do I skip a blank cell? #2196
-
To keep it short, I'm trying to store the values from the record “HoursWorked” as int variable. However, the CSVReader keeps bumping into an error when it catches a blank cell, causing the program to crash. Here are my C# source files, Application.cs: using CsvHelper;
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;
using System.Globalization;
namespace Inheritence {
public abstract class Application {
private static void Main() {
var readFile = new StreamReader(Path.Join("res", "employees.csv"));
// var csvHelperTypeConverterOptions = new TypeConverterOptions { NullValues = { string.Empty, "''" } };
var csvHelperConfig = new CsvConfiguration(CultureInfo.CurrentCulture) {
Delimiter = ";", HasHeaderRecord = true, MissingFieldFound = null,
IgnoreBlankLines = true
};
var readCsvData = new CsvReader(readFile, csvHelperConfig);
var csvRecords = readCsvData.GetRecords<Employees>();
foreach (var rec in csvRecords) {
Console.WriteLine($"Employee ID: {rec.EmployeeID}");
Console.WriteLine($"Name: {rec.Name}");
Console.WriteLine($"Address: {rec.Address}");
Console.WriteLine($"Phone: {rec.Phone}");
Console.WriteLine($"SSN: {rec.SSN}");
Console.WriteLine($"Date of Birth: {rec.DateOfBirth}");
Console.WriteLine($"Department: {rec.Department}");
Console.WriteLine($"Salary: {rec.Salary}");
Console.WriteLine($"Hours Worked: {rec.HoursWorked}\n");
}
}
}
} Employees.cs // This will be used for accessing the Employees class.
namespace Inheritence {
// public sealed class TheEmployee {
// public int Salaried;
// public int PartTime;
// public int Wages;
// }
// ReSharper disable once ClassNeverInstantiated.Global
/// For information about employees.
public class Employees {
// ReSharper disable all UnusedAutoPropertyAccessor.Global (DO NOT REMOVE THIS COMMENT)
// ReSharper disable all InconsistentNaming (DO NOT REMOVE THIS COMMENT)
/// The employee ID.
public int EmployeeID { get; set; }
/// The name of the employee.
public string Name { get; set; }
/// The address of the employee.
public string Address { get; set; }
/// The phone number of the employee.
public string Phone { get; set; }
/// The SSN(Social Security Number) of the employee.
public string SSN { get; init; }
/// The employee's date of birth.
public string DateOfBirth { get; set; }
/// The department the employee currently works.
public string Department { get; set; }
/// The employee's salary.
public double Salary { get; set; }
/// The number of hours the employee has worked.
public int HoursWorked { get; set; }
}
} Also, here is the CSV file I am trying to parse (the file is autogenerated, it doesn't contain any information about any real people): Error log:
|
Beta Was this translation helpful? Give feedback.
Answered by
AltruCoder
Sep 30, 2023
Replies: 1 comment 2 replies
-
You have two choices.
public int? HoursWorked { get; set; }
[Default(0)]
public int HoursWorked { get; set; } |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
FlawlessCasual17
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have two choices.
HoursWorked
aNullable<int>
. Then any blank cells will come in as a null value.HoursWorked
. Then any blank cells will come in as the default value you have set.