0

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.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111

2 Answers2

1

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

Community
  • 1
  • 1
pengMiao
  • 324
  • 1
  • 6
  • I know that I can use `dynamic` only on 4.0 upwards. However I´m not using the keyowrd as you see within my second update. – MakePeaceGreatAgain Jan 22 '16 at 10:54
  • 1
    Based on http://www.mikesdotnetting.com/article/214/how-to-check-if-a-query-returns-data-in-asp-net-web-pages, when you use `queryvalue` or `querysingle` it actually return a `dynamic` object – pengMiao Jan 22 '16 at 11:03
  • which version of ironpython you are using? – pengMiao Jan 22 '16 at 11:07
  • @HimBromBeere can you post the code of your row.getValue() or information about any third party library(include nuget package) or assemblies use so that we can dig up mor info? – pengMiao Jan 22 '16 at 11:16
  • The code is shipped within ArcGIS10 for Desktop- However I´m quite sure they do NOT return any dynamic, as the assembly is in use for years now without ever noticing such a behaviour on even .NET 2.0. – MakePeaceGreatAgain Jan 22 '16 at 11:20
  • @HimBromBeere sorry i just found i miss a very important information from your question. the problem is as you suspected : referencing Microsoft.Scripting and Microsoft.Scripting.Core. Ironpython has its own version of Microsoft.Scripting and Microsoft.Scripting.Core , so when you try to reference the offlcial Microsoft.scipting and microsoft.scripting.core into your project, it may conflict and causes weird behaviour just like your case. So, please remove the reference. Hope its help :) – pengMiao Jan 22 '16 at 11:30
  • I replaced the assemblies by those shipped within IronPython but it also changed nothing. – MakePeaceGreatAgain Jan 22 '16 at 11:53
  • Obvioulsy you assumed right that the call to `row.Get_Value` actually retusn a `dynamic` instead of an `object`. This made my way, thanks – MakePeaceGreatAgain Jan 22 '16 at 13:19
0

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.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111