MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

Cannot convert Db-date to DateTime

I have a date from a database as shown in the image below. DateTime-image

However when I convert this value to a DateTime-instance I get the error shown below. enter image description here

However when running the code I get an InvalidCastException that the returned value cannot be cast to DateTime.

I am using VS2010 on .Net3.5 that´s why I wonder why this library is needed at all.

Within a referenced assembly I´m using IronPython which uses DLR as far as I know. On .Net 3.5 I can use this however when referencing Microsoft.Scripting and Microsoft.Scripting.Core also. I suppose this causes the error in any way. Weird on this is that the code that uses IronPython is not called at all when executing the code producing that exception.

EDIT: For those who can´t believe: the same code works some time querying some rows from the database and returning correct dates. However at one point it throws that exception.

Further EDIT: As you can see on first image row.GetValue returns a dynamic which is strange as the assembly (ArcGIS 10.2 for Desktop) where this code is implemented is also written using .Net 3.5. Furthermore when I go to the definition of that type I get object get_Value(int Index); instead of dynamic.

Upvotes: 0

Views: 515

Answers (2)

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

I found some really strange behaviour which explains the exceptions.

First of all the InvalidCastException was absolutely right as the call to row.Get_Value(2) actually returned DBNull which of course cannot be cast to DateTime.

Next the value shown in the QuickWatch wasn´t the right one, as VS showed me the whrong line for that exception making me assume that the call to (DateTime) row.Get_Value(2) caused the error instead of the call to row.Get_Value(1) (which preceds the former). The latter however returns DBNull as allready mentioned.

Last but not least the messy stuff around with the missing library is obviously caused by ArcGIS itself. As @pengMiao already assumed this call actually returns a dynamic (although its signature only mentions object). Now QuickWatch isn´t smart enough to directly cast this to whatever the right type is (in my case DateTime). However if I write (DateTime)(object) row.Get_Value(2) providing a cast to object first and then to DateTime we get what we want in both, QuickWatch AND executable.

It is still unclear to me why in QuickWatch we get dynamic { DateTime } as data-type for the value returned, however it works now.

Upvotes: 0

pengMiao
pengMiao

Reputation: 334

you need at least .NET framework 4.0 to use dynamic. The compiler will allow you to define and assign a dynamic variable, but when you trying to do anything with the dynamic variable, you will come across this error

Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported

Please refer to C# 4.0 and .Net 3.5 for more details. credit goes to that Aaronaught ( which answer that question)

Update:

on .Net 3.5 I can use this however when referencing Microsoft.Scripting and Microsoft.Scripting.Core also. I suppose this causes the error in any way.

IronPython have their own customized Microsoft.Scripting and Microsoft.Scripting.Core. When you reference those two assemblies again, it will cover up IronPythons' dll and use those official which i believe causing those weird behaviour. Try to remove both reference and see does it helps

Upvotes: 1

Related Questions