locked
single if statement doesn't work RRS feed

  • Question

  • User1094877758 posted

    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
     

    Friday, September 19, 2014 1:38 PM

Answers

  • User-484054684 posted

    I was parallely editing, did you try with (object) ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 19, 2014 1:46 PM

All replies

  • User-484054684 posted

    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

    Friday, September 19, 2014 1:42 PM
  • User1094877758 posted

    Tried that as well. Same error

    Friday, September 19, 2014 1:45 PM
  • User-484054684 posted

    I was parallely editing, did you try with (object) ?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 19, 2014 1:46 PM
  • User1094877758 posted

    That worked. Thanks.

    I tried explicitly casting the DBNull.Value to a (string). But it gave the same error.

    Friday, September 19, 2014 1:51 PM
  • User-484054684 posted

    Ok, No problem. As the DBparameter.Value expects the type as object, the above statement will expect to cast to object.

    Friday, September 19, 2014 1:53 PM
  • User1094877758 posted

    Thanks for the explaination.

    Friday, September 19, 2014 2:06 PM
  • User-484054684 posted

    You're welcome, Have a nice day

    Friday, September 19, 2014 2:47 PM