• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 542
  • Last Modified:

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

0
kwh3856
Asked:
kwh3856
  • 4
  • 2
1 Solution
 
abelCommented:
The ?? should be used on a nullable type. You are currently not using a nullable type but a normal type.
0
 
kwh3856Author Commented:
Thank you.  I did not think about that.  That is what happens when you tend to copy and paste code:)

Thanks
Kenny
0
 
abelCommented:
My mistake. Try this:




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

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
abelCommented:
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)
0
 
abelCommented:
> but it wasn't deserved
yet ;)
0
 
kwh3856Author Commented:
Thanks.  But I disagree:)
You solved my problem:)
Thanks
Kenny
 
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now