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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top