Cassie Mod

Cassie Mod

  • NA
  • 488
  • 70.1k

string is nog recognized as a valid DateTime

Feb 25 2016 11:34 AM
HI i made the followin class to get dates from an excel file.
 
file:
        col:a      col:b                      col:c 
1.   X-mas      2015-19-12         2016-03-01 
2.  carnaval   2016-20-02         2016-28-02
 
  1. using System;  
  2. using OfficeOpenXml;  
  3. using System.IO;  
  4.   
  5.   
  6. namespace CalviVsTribessCheck.Cmd  
  7. {  
  8.     public static class ExcelFileDataAccess  
  9.     {  
  10.         public static void ReadExcelFile(DateTime start)  
  11.         {  
  12.             var filePath = @"c:\code\Tools\Development\RtcMonitoring\RatingTribessCheck.Cmd\Vacations.xlsx";  
  13.             var sheetName = "vacations";  
  14.             using (var excelPkg = new ExcelPackage())  
  15.             using (var stream = new FileStream(filePath, FileMode.Open))  
  16.             {  
  17.                 excelPkg.Load(stream);  
  18.                 ExcelWorksheet oSheet = excelPkg.Workbook.Worksheets[sheetName];  
  19.   
  20.                 var totalRows = oSheet.Dimension.End.Row;  
  21.   
  22.                 for (var i = 1; i <= totalRows; i++)  
  23.                 {  
  24.                       
  25.                     var vacationbegindate = DateTime.Parse((oSheet.Cells[i, 1]).Value.ToString());  
  26.                     var vacationenddate = DateTime.Parse((oSheet.Cells[i, 2]).Value.ToString());  
  27.   
  28.                     BusinessLogic.CheckVacation(start, vacationbegindate, vacationenddate);  
  29.   
  30.                 }  
  31.             }  
  32.         }  
  33.   
  34.   
  35.   
  36.   
  37.   
  38.   
  39.   
  40.   
  41.     }  
  42. }  
however i get the following error when converting a cell value to a dateTime:
 
 The string is not recognized as a valid DateTime. Index 0 begins with an unknown word
 
How can i fix this ???? please provide me with your answer 

Answers (4)