E
ESPNSTI
Hi,
Please don't shoot me if the answer is simple, I'm new to .Net and C#
..
I'm attempting to convert a nullable type to it's "non-nullable" type in a
generic way (without knowing what specific type the nullable type is.)
The reason I'm trying this is because when I attempt to pass a nullable type
value to a SqlCommand parameter and then attempt to execute it I the
following error:
"No mapping exists from object type System.Nullable`1[[System.DateTime,
mscorlib, Version=2.0.3600.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]] to a known managed provider native type."
If you take a look below, I think I found a way to at least detect if
"value" is a Nullable type or not. (Not sure if that's the right way to do
it.)
However I haven't found a way to typecast the "value" variable so that I can
use the Nullable type's methods and properties.
When I try "(Object?)value", I get a "Specified cast is not valid." /
"Cannot unbox 'value' as a 'object?'" error.
How do I typecast the value variable so that I can access the Nullable
methods and properties?
(I know (DateTime?)value works, but I don't know that it's a DateTime
nullable type and I don't want to end up with a big case statement for a
bunch of types).
Thanks,
Erik
The code:
____________________________________________________________________________
_____________
// Setting it to a DateTime? nullable value here, in my actual code I
don't know that it's a DateTime? type.
object value = (DateTime?)DateTime.Parse("01/01/2005");
Type valueType = value.GetType();
// Check if it is a nullable type.
if (valueType.HasGenericArguments &&
valueType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
// See if it has a value.
if (((Object?)value).HasValue) //<-- This doesn't work, get a "Cannot
unbox 'value' as a 'object?'" error
// It does have a value, "convert" the value variable to a
"non-nullable" type.
value = ((Object?)value).ToObject();
else
// It doesn't have a value, use null
value = null;
____________________________________________________________________________
_____________
Please don't shoot me if the answer is simple, I'm new to .Net and C#

..
I'm attempting to convert a nullable type to it's "non-nullable" type in a
generic way (without knowing what specific type the nullable type is.)
The reason I'm trying this is because when I attempt to pass a nullable type
value to a SqlCommand parameter and then attempt to execute it I the
following error:
"No mapping exists from object type System.Nullable`1[[System.DateTime,
mscorlib, Version=2.0.3600.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]] to a known managed provider native type."
If you take a look below, I think I found a way to at least detect if
"value" is a Nullable type or not. (Not sure if that's the right way to do
it.)
However I haven't found a way to typecast the "value" variable so that I can
use the Nullable type's methods and properties.
When I try "(Object?)value", I get a "Specified cast is not valid." /
"Cannot unbox 'value' as a 'object?'" error.
How do I typecast the value variable so that I can access the Nullable
methods and properties?
(I know (DateTime?)value works, but I don't know that it's a DateTime
nullable type and I don't want to end up with a big case statement for a
bunch of types).
Thanks,
Erik
The code:
____________________________________________________________________________
_____________
// Setting it to a DateTime? nullable value here, in my actual code I
don't know that it's a DateTime? type.
object value = (DateTime?)DateTime.Parse("01/01/2005");
Type valueType = value.GetType();
// Check if it is a nullable type.
if (valueType.HasGenericArguments &&
valueType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
// See if it has a value.
if (((Object?)value).HasValue) //<-- This doesn't work, get a "Cannot
unbox 'value' as a 'object?'" error
// It does have a value, "convert" the value variable to a
"non-nullable" type.
value = ((Object?)value).ToObject();
else
// It doesn't have a value, use null
value = null;
____________________________________________________________________________
_____________