Search code examples
c#.netreflectionstrongly-typed-dataset

Changing data type of field in .net


Is it possible to change the data type of a field/property in .net through reflection or something?

I am looking for this feature, as there is problem in Strongly Typed Datasets. Strongly Typed Datasets don't give You the flexibility of changing data types of nullable types. See a related link here. So, if there is a possibility to change the data type at run time, this problem can be eliminated. Also, to add; as per this MSDN Link; Microsoft team is not very keen to provide this support.


Solution

  • First, this is not a bug; it's due to the difference between dbnull ie unknown value and null ie a reference pointing nowhere. It's compounded by the fact that Microsoft themselves seem to be poor at explaining it, judging by some of the opaque and unhelpful responses on Connect and elsewhere.

    There are a number of solutions to this issue, depending on what you are trying to achieve. If you are using databinding, then the binding mechanism mostly handles this transparently for you; if you are hitting the dataset directly in code the designer will have built Isxxxnull / setxxxnull methods for you to check the value before you use it. Finally you can change the datatype to system.object, or you can overtype the NullValue in the designer to enter another value if you wish, as long as that value is valid for the datatype - eg 0 or -1 for an int.