I am using LinqToExcel to import large Excel data sheet into SQL Server database table Employees2. I am using EF4. The Date field in Excel (DOB and many other) is in "MMddyyyy" format. I have many fields in Excel, I just show 1 mapping here.
var excel = new ExcelQueryFactory(excelPath);
excel.AddMapping<Employee2>(x => x.DOB, "DOB",
s => String.IsNullOrEmpty(s) ? DateTime.MinValue :
DateTime.ParseExact(s, @"MMddyyyy", CultureInfo.CurrentCulture.DateTimeFormat));
var employees = excel.Worksheet<Employee2>("Sheet1").ToList();
employees.ForEach(e => context.Employee2.AddObject(e));
context.SaveChanges();
The code throws exception "String was not recognized as a valid DateTime."
on this line of code: var employees = excel.Worksheet("Sheet1").ToList();
My question is: How can I capture which record exactly causes the exception?
The exception doesn't say which record or even which field.
Just to make sure the conversion is fine, in command window I ran:
DateTime.ParseExact("08012012", "MMddyyyy", CultureInfo.CurrentCulture.DateTimeFormat)
Update:
Found the solution myself: EF Exception: String or binary data would be truncated. The statement has been terminated.?