c# - Using Conditional operator inside a string? -


how use conditional operator inside string?

why value of string missing to_date( , ' single quote? , can fix it?

stringbuilder sb = new stringbuilder(); //code... sb.appendline("     '" + txtstatus.text + "',"); sb.appendline("     to_date(" + dtpeligdate.value.tostring("yyyymmddhhmmss") == "" ? "null" : dtpeligdate.value.tostring() + "),"); sb.appendline("     '" + txtcoverageendreason.text == "" ? "null" : txtcoverageendreason.text + "',"); //code... 

string value:

'', 7/19/2013 9:04:35 am), ', 

my understanding not caused lack of escaping charecters, because of use of conditional operator.

thanks help!

it's because of operator precedence, add () ternary operator , work:

sb.appendline("to_date(" +      (dtpeligdate.value.tostring("yyyymmddhhmmss") == "" ? "null" : dtpeligdate.value.tostring()) +  "),"); 

basicaly first concatenate strings (+) , evaluate conditional statement (?:), expect opposite. parentheses fix it.

as side note: it'd better use parametrized queries.


Comments