c# - ParseExact doesnt recognise a string extracted from a DataTable -


i writing integration test , such, reading csv file in datatable, , trying parse elements in each row. when string representation of date, , pass datetime.parseexact error: "string not recognised valid datetime"

when use commented-out line in code provide here, , explicitly define date string, parseexact works fine. when debug integration test startdatestring has correct date value.debug capture of value datatable when passed parseexact reason throws error.

i have tried use explicit culture, eg. "en-gb", , datestyles.none, without success. have idea, why string input datarow not accepted datetime.parseexact constructor?

thanks.

... var datatable= file.readfile("file.csv", 2);             (int = 0; < datatable.rows.count / 2; = + 2)              {                 var startdate = new datetime();                 var maturity= new datetime();                 try                 {                     var startdatestring = datatable.rows[i]["item9"].tostring();  //                    var startdatestring = "24/01/2008";                     var formats = new[] { "dd/m/yyyy", "dd/mm/yyyy" };                     startdate = datetime.parseexact(startdatestring, formats, cultureinfo.invariantculture,                                         datetimestyles.assumelocal);                     enddate = datetime.parseexact(datatable.rows[i]["item10"].tostring(), formats, cultureinfo.invariantculture,                                         datetimestyles.assumelocal);                  }                 catch (exception e)                 {                     throw new formatexception(string.format("there error format of 1 of dates in file"));                   } ... 

your startdatestring has trailing space in it.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -