Also, you cannot use ?: operator to do something like this:
(Assume yourParameter is the variable that contains some value, and yourSqlParameter is the variable you want to pass into SqlCommand as the parameter.
SqlParameter yourSqlParameter = new SqlParameter("@FieldName", SqlDbType.CertainDbType);
yourSqlParameter.Value = (yourParameter == null) ? DBNull.Value : yourParameter;
because yourParameter type is not compatible with DBNull.Value. In the MSDN documentation for the
?:
operator states the following:Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.
Therefore, we need to do something like:
if (yourParameter == null)
{
yourSqlParameter.Value = DBNull.Value;
}
else
else
{
yourSqlParameter.Value = yourParameter;
}