I have an asp.net text form that contains numerous decimal fields that are optional. I want to selectively update the database but not inserting a ‘0’ for fields that do not have data (maintaining the null status).
Typically, I would create multiple functions, each with a different signature to handle this. However, I am inserting the data through a webservice which does not allow a function with the same name to have multiple signatures. I can think of a couple ways to work around this, but none ‘pragmatically’.
Nullable Types are meant for the same purpose. They represent value types with the possibility of having no data in them. Presence of value can be checked using HasValue property of these types.
Pseudo code to read the fields:
When you say multiple methods – I assume these are overloaded methods to be able to add optional fields (so n optional fields means n more methods)
You can avoid writing those methods by writing single method. Suppose you have one required field and one optional field:
Then define the web service method to use it:
If the nullable type has a value, Null Coalescing Operator will set the value or else it will set the other value that you specify (DBNull.Value in our case).