14

Taken directly from the immediate window:

reader["DateDue"] as DateTime? yields:

'reader["DateDue"] as DateTime?' threw an exception of type 'System.NullReferenceException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147467261
HelpLink: null
InnerException: null
Message: "Object reference not set to an instance of an object."
Source: null
StackTrace: null
TargetSite: null

(DateTime?)reader["DateDue"] yields:

{1/26/2015 12:00:00 AM}
Date: {1/26/2015 12:00:00 AM}
Day: 26
DayOfWeek: Monday
DayOfYear: 26
Hour: 0
Kind: Unspecified
Millisecond: 0
Minute: 0
Month: 1
Second: 0
Ticks: 635578272000000000
TimeOfDay: {System.TimeSpan}
Year: 2015

and for reference, reader["DateDue"] yields:

{1/26/2015 12:00:00 AM}
Date: {1/26/2015 12:00:00 AM}
Day: 26
DayOfWeek: Monday
DayOfYear: 26
Hour: 0
Kind: Unspecified
Millisecond: 0
Minute: 0
Month: 1
Second: 0
Ticks: 635578272000000000
TimeOfDay: {00:00:00}
Year: 2015

Is this a bug? If directly casting to DateTime? works then casting with as DateTime? should work too.

I did find a work around for this by using reader.GetDateTime(reader.GetOrdinal("DateDue")) as DateTime?. Nevermind, that doesn't handle nulls well. Anyway there's myriad ways to paper around this oddity.

Repo demonstrating the issue can be found here: https://github.com/jjoedouglas/exceptionAsDatetime

MushinNoShin
  • 4,695
  • 2
  • 32
  • 46
  • It's a very simple `IDataReader` in a tight loop. Not interesting in the slightest, if you've ever written code that uses `SqlConnection` and `SqlCommand`, you've seen it a million times. – MushinNoShin Jul 27 '15 at 04:42
  • 1
    @MushinNoShin "It's a very simple IDataReader in a tight loop". Except your 'very simple loop' is giving an error that doesn't make sense. Show us the code, don't just say using the `as` keyword is throwing a null reference exception. – Rob Jul 27 '15 at 05:16
  • I agree that your question could have been better. But I did reproduce your error, and I will admit that I am stumped. I only get the error if I am trying to read a `DateTime` value from a `SqlDataReader`, and only if I'm doing it from an `Immediate` window. No idea why. – sstan Jul 27 '15 at 05:22
  • I'm working on putting a simple repo up on github. Showing some code isn't going to help at all since you'd also want to see the sql schema and probably be able to run it too. – MushinNoShin Jul 27 '15 at 05:25
  • I could get the error at runtime when casting to `DateTime?`, not just immediate window. At least.. I think. It is 1am here. – MushinNoShin Jul 27 '15 at 05:26
  • hmm, I do only see it in the immediate window. Sounds like a vs2015 bug. – MushinNoShin Jul 27 '15 at 06:00
  • @Rob, repo is up. You'll see it's as simple as I said it was. – MushinNoShin Jul 27 '15 at 06:22
  • @MushinNoShin I did get it to happen once, but since then haven't been able to reproduce (it correctly prints out `null` now when I try it). Still looking into it.. – Rob Jul 27 '15 at 06:40
  • @Rob, yeah, I couldn't get it to repro while running it. Only in the immediate window. This may be a VS2015 issue, I just assume I'm wrong before I blame it on bugs.. hence this SO question :/ Sorry for what looks like not a very actionable question. – MushinNoShin Jul 27 '15 at 07:09
  • 1
    I can reproduce this all the time using VS2015, correctly prints null in VS2013. Only in immediate window, execution works fine in both. – Adrian Fâciu Jul 27 '15 at 07:23
  • Not getting any exception in LinqPad as well – TheVillageIdiot Jul 28 '15 at 00:24
  • 2
    I wonder if this is related to [this question](http://stackoverflow.com/questions/31562791) - quote from answer: "To support lambda expressions, we had to completely rewrite the CLR Expression Evaluator in Visual Studio 2015. " – Blorgbeard Jul 28 '15 at 21:40
  • 1
    There's a much simpler repro in VS2015 that doesn't require a project. `(object)DBNull.Value as DateTime?` triggers it any time. `new object() as int?` triggers the same [incorrect] behavior. – recursive Jul 31 '15 at 23:08
  • @BhavO When boxing a `DateTime?` you get a `DateTime` in a box, not a `DateTime?` in a box. The `Nullable<>` type has special boxing conventions. Since both `x is T` and `x.GetType()` (where the declared (compile-time) type of `x` is a value type) lead to boxing of `x`, the behavior you see is correct. – Jeppe Stig Nielsen Jul 31 '15 at 23:17
  • yes i recall now, i remember reading about the special treatment of nullables, nice one – BhavO Jul 31 '15 at 23:20

1 Answers1

2

This appears to be a bug in the VS2015 immediate window.

MushinNoShin
  • 4,695
  • 2
  • 32
  • 46