Link to home
Start Free TrialLog in
Avatar of kwh3856
kwh3856Flag for United States of America

asked on

Operator '??' cannot be applied to operands of type 'string' and 'int'

I am getting this error message when trying to save data from a variable into a field in a table.  What I do not under stand is the field type in the database is INT and I am also performing an INT conversion.  Why is it giving me this error if both are integers?

Here is the code.
myPatPhones.SourceOfRecord = Convert.ToInt32(patient.d.NPI ?? 0);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwh3856

ASKER

Thank you.  I did not think about that.  That is what happens when you tend to copy and paste code:)

Thanks
Kenny
My mistake. Try this:




myPatPhones.SourceOfRecord = Convert.ToInt32(patient.d.NPI ?? "0");

Open in new window

Thanks for the A, but it wasn't deserved. I delved a bit deeper in it and found the above. The thing is this:

  • The ?? operator can be used with nullable types, but then the left-hand and right-hand side must be of the same type
  • The ?? operator can also be used with reference types (like a string). But then, too, the left-hand and the right-hand side must be of the same type.
  • The ?? operator can not be used with nullable strings... Don't know why.
This info is from some tests and MSDN. (hope you still read my update after having closed the question. Let me know if it works for you)
> but it wasn't deserved
yet ;)
Avatar of kwh3856

ASKER

Thanks.  But I disagree:)
You solved my problem:)
Thanks
Kenny