conditional c#

  • Thread starter Thread starter dave
  • Start date Start date
D

dave

while adding paramters for a stored procedure i have the
following line

arParams[2] = new SqlParameter("@TopicID", (TopicID==-1)?
System.DBNull.Value:TopicID);

this does not compile with the following error

"Type of conditional expression can't be determined
because there is no implicit conversion
between 'System.DBNull' and 'int'"

How best can I fix this (besides the long for if/else
format)?

Thank you in advance
dave
 
dave said:
while adding paramters for a stored procedure i have the
following line

arParams[2] = new SqlParameter("@TopicID", (TopicID==-1)?
System.DBNull.Value:TopicID);

this does not compile with the following error

"Type of conditional expression can't be determined
because there is no implicit conversion
between 'System.DBNull' and 'int'"

How best can I fix this (besides the long for if/else
format)?

Cast both expressions to the type you want, eg

arParams[2] = new SqlParameter ("@TopicID", (TopicID==-1) ?
(object)DBNull.Value : (object)TopicID);
 
Back
Top