Last post Sep 19, 2014 02:47 PM by Siva Krishna Macha
Member
62 Points
269 Posts
Sep 19, 2014 01:38 PM|rds80|LINK
How come this works?
if (string.IsNullOrEmpty(smeID)) { loParam[0].Value = DBNull.Value; } else { loParam[0].Value = smeID; }
But this doesn't
loParam[0].Value = string.IsNullOrEmpty(smeID) == true ? DBNull.Value : smeID;
The latter gives the following error:
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'
Thanks for the help
Star
9052 Points
2255 Posts
Sep 19, 2014 01:42 PM|Siva Krishna Macha|LINK
loParam[0].Value = string.IsNullOrEmpty(smeID) == true ? (object)DBNull.Value : smeID;
The above ternary operator gets confused with the datatype. So, we need to convert it to help it to identify the type.
Reference: http://stackoverflow.com/a/16503321
Sep 19, 2014 01:45 PM|rds80|LINK
Tried that as well. Same error
Sep 19, 2014 01:46 PM|Siva Krishna Macha|LINK
I was parallely editing, did you try with (object) ?
Sep 19, 2014 01:51 PM|rds80|LINK
That worked. Thanks.
I tried explicitly casting the DBNull.Value to a (string). But it gave the same error.
Sep 19, 2014 01:53 PM|Siva Krishna Macha|LINK
Ok, No problem. As the DBparameter.Value expects the type as object, the above statement will expect to cast to object.
Sep 19, 2014 02:06 PM|rds80|LINK
Thanks for the explaination.
Sep 19, 2014 02:47 PM|Siva Krishna Macha|LINK
You're welcome, Have a nice day
Member
62 Points
269 Posts
single if statement doesn't work
Sep 19, 2014 01:38 PM|rds80|LINK
How come this works?
But this doesn't
The latter gives the following error:
Type of conditional expression cannot be determined because there is no implicit conversion between 'System.DBNull' and 'string'
Thanks for the help
Star
9052 Points
2255 Posts
Re: single if statement doesn't work
Sep 19, 2014 01:42 PM|Siva Krishna Macha|LINK
loParam[0].Value = string.IsNullOrEmpty(smeID) == true ? (object)DBNull.Value : smeID;
The above ternary operator gets confused with the datatype. So, we need to convert it to help it to identify the type.
Reference: http://stackoverflow.com/a/16503321
Thanks & Regards,
Siva
Member
62 Points
269 Posts
Re: single if statement doesn't work
Sep 19, 2014 01:45 PM|rds80|LINK
Tried that as well. Same error
Star
9052 Points
2255 Posts
Re: single if statement doesn't work
Sep 19, 2014 01:46 PM|Siva Krishna Macha|LINK
I was parallely editing, did you try with (object) ?
Thanks & Regards,
Siva
Member
62 Points
269 Posts
Re: single if statement doesn't work
Sep 19, 2014 01:51 PM|rds80|LINK
That worked. Thanks.
I tried explicitly casting the DBNull.Value to a (string). But it gave the same error.
Star
9052 Points
2255 Posts
Re: single if statement doesn't work
Sep 19, 2014 01:53 PM|Siva Krishna Macha|LINK
Ok, No problem. As the DBparameter.Value expects the type as object, the above statement will expect to cast to object.
Thanks & Regards,
Siva
Member
62 Points
269 Posts
Re: single if statement doesn't work
Sep 19, 2014 02:06 PM|rds80|LINK
Thanks for the explaination.
Star
9052 Points
2255 Posts
Re: single if statement doesn't work
Sep 19, 2014 02:47 PM|Siva Krishna Macha|LINK
You're welcome, Have a nice day
Thanks & Regards,
Siva