Question

I would like to understand what the best way of dealing with the following scenario is:

I have old serialized instances of my DataSet persisted to disk. I am introducing a new field on the DataSet.

enter image description here

When the older xml versions of the DataSet are read in, they are read in as DBNulls. It attempts to cast the DBNull to an int and barfs on trying to access the value in the field.

The behavior I desire is that it would default to 0 if it can't find the element corresponding to the new field in the serialized DataSet.

The options I have identified are:

(1) Wherever I try to access the field, I can setup a catch block (InvalidCastException) and set the field's value to 0. (2) Change the DataSet.Designer.cs to change the behavior on seeing DBNulls. The file is auto-generated though and I don't think its a good place to make the change.

I don't like (1) or (2) and I am wondering if there is a more elegant solution out there.

Any suggestions/help would be greatly appreciated!

Était-ce utile?

La solution

Discovered that there is a very simple and elegant solution out there.

All I needed to do was to flip the AllowDBNull property to have value 'False' instead of 'True'.

enter image description here

I wish this behavior had been documented more explicitly here: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.allowdbnull(v=vs.110).aspx

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top