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 Nevermind, that doesn't handle nulls well. Anyway there's myriad ways to paper around this oddity.reader.GetDateTime(reader.GetOrdinal("DateDue")) as DateTime?
.
Repo demonstrating the issue can be found here: https://github.com/jjoedouglas/exceptionAsDatetime
Use the Immediate window to debug and evaluate expressions, execute statements, and print variable values. The Immediate window evaluates expressions by building and using the currently selected project.
Press Ctrl + Q to focus on Quick Launch text box. Type Immediate then choose the item that appears: Debug -> Windows -> Immediate ( Ctrl+Alt+I )
Working With Variables To view the variable's value, simply type its name into the immediate window and press Enter. The value, "Hello world" will be displayed beneath the typed line, as in the image above. To access properties of a variable or value, use the member access operator (.) as you would within source code.
This appears to be a bug in the VS2015 immediate window.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With