1

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?

1 Answer 1

2

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") 
Sign up to request clarification or add additional context in comments.

4 Comments

This seems to do the trick and it ticks to the next lines. The next BSyncDateTime is being skipped though but this is propably because all those values are NULL. Could the wrong cast have something to do with it's previous database being Access and not SQL (just migrated it from Access to SQL)? I think I can also ommit the parse now right? Got it like this now: let BLastDateTime = b.Field<system.Datetime>("BLastDateTime") == null ? (DateTime?)null : b.Field<System.DateTime>("BLastDateTime")
Just changed the BSyncDateTime with the same system.DateTime but this doesn't seem to work when there are NULL values since I get the error: Can't calculate a DBNull.Value for type System.DateTime. Use a null adjustable type.
Now this is a different error. Your c# filed should be nullable to assign a null value or you should check for null and replace it with something else.
Thank you! I fixed the null error by using system.DateTime? !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.