简体   繁体   中英

InvalidCastException DateTime to String C#

So I've got the fun assignment to check someone else his code and stumbled immediately on a problem when trying to run the web application. When I click a certain button I am getting an Invalid Cast Exception from DateTime to String . I've read already many posts on this and tried several things but non of them seem to work or I don't know how I can apply it in this specific code.

I addressed the problem to the original code creator but on his system the code DOES work (while we use the same database and visual studio). So this made me believe it was a culture setting issue (which was quite possible since he lives in India and I in the Netherlands). I tried some things with setting the culture info but this didn't seem to work.

I really don't know what is exactly going wrong so below I posted the findings, tried adjustments, the code outputs and code explanation:

In the web application a user can click a specific order number and when they do that they probably see some details of that order. Though when clicked on it I get the cast exception on this:

var updatedBookedHourData = (from b in bookedHourList
                                         let BLastDateTime = b.Field<string>("BLastDateTime") == null ? (DateTime?)null : DateTime.Parse(b.Field<string>("BLastDateTime"))
                                         let BSyncDateTime = b.Field<string>("BSyncDateTime") == null ? (DateTime?)null : DateTime.Parse(b.Field<string>("BSyncDateTime"))
                                         where !BLastDateTime.HasValue ||
                                         !BSyncDateTime.HasValue ||
                                         BLastDateTime > BSyncDateTime
                                         select new
                                         {
                                             Ordernumber = b.Field<int>("Ordernumber"),
                                             Position = b.Field<string>("Position"),
                                             Rate = b.Field<string>("Rate"),
                                             WorkType = b.Field<string>("WorkType")
                                         }).ToList();

The cast exception is on this specific line:

let BLastDateTime = b.Field<string>("BLastDateTime") == null ? (DateTime?)null : DateTime.Parse(b.Field<string>

So next I did some research. First I looked what was the output of the VAR UpdatedBookedHourData like so:

var updatedBookedHourData3 = (from b in bookedHourList
                                          select b).ToList();

this resulted in ~400 records and also the right datetime values: 21-7-2015 15:32:22 . I also tested the query of the var in the SQL server and there I also get a valid output. The BLastDateTime column in the SQL server lists it's dates as follow though: 2014-07-21 15:32:22 000 (no clue this has any influence). I also checked the datetype in the SQL server and the column has datetime2 as its type.

Next I tried to get a list on the first statement like so:

var updatedBookedHourData2 = (from b in bookedHourList
                                         select new {test = b.Field<string>("BLastDateTime") }).ToList();

This resulted in the same invalid cast exception (datetime to string) . There were by the way 0 NULL values in the SQL database on the BLastDateTime column.

Because it all worked on the creator his system I tried to do something with culture info:

I tried all of the following:

let BLastDateTime = b.Field<string>("BLastDateTime") == null ? (DateTime?)null : DateTime.Parse(b.Field<string>("BLastDateTime"),CultureInfo.InvariantCulture)

let BLastDateTime = b.Field<string>("BLastDateTime") == null ? (DateTime?)null : DateTime.Parse(b.Field<string>("BLastDateTime"),CultureInfo.CurrentCulture)

let BLastDateTime = b.Field<string>("BLastDateTime") == null ? (DateTime?)null : DateTime.Parse(b.Field<string>("BLastDateTime"),CultureInfo.CreateSpecificCulture("en-US"))

All of these resulted in the InvalidCastException .

I am kinda out of ideas right now, perhaps anyone knows where it goes wrong?

It could be a DATE / DATETIME field in the database. As per error message, it's a casting issue. You need to cast it to C# equivalent System.DateTime instead of String

b.Field<System.DateTime>("BLastDateTime") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM