Datetime DbNull values in Dataset not deserialized correctly when RemotingFormat is set to Binary -
11-02-2006
, 11:16 AM
Hi,
when using BinaryFormatter to serialize and deserialize a DataSet that
has RemotingFormat set to SerializationFormat.Binary, all the DateTime
fields that have DBNull value are changed to 01.01.0001 01:00
(DateTime.MinValue). This does not happed when the RemotingFormat is
not Binary.
I believe this is a bug - the deserialized object is not the same as
the original.
Also, it is impossible to store such corrupted dataset to a database,
01.01.0001 value causes an overflow exception in the SqlDateTime value.
The binary (de)serialization is much faster than the 'xml'
(de)serialization, we would like to use it, but we do not want to
repair every datetime value in every row of every table after the
dataset has been transfered through remoting or loaded from a file.
Please, does anyone know a workaround for this ?
Thank you!
Peter
Example:
// DataSet2 is a simple one table dataset.
// The DataTable1 table contains only one DateTime field that
// is DBNull by default.
// Add a test row
DataSet2 ds = new DataSet2();
DataSet2.DataTable1Row row = ds.DataTable1.NewDataTable1Row();
ds.DataTable1.AddDataTable1Row(row);
// Use binary format
ds.RemotingFormat = SerializationFormat.Binary;
// Serialize
// Now the value of DateTime field in DataTable1 is DBNull
MemoryStream ms = new MemoryStream();
BinaryFormatter fmt = new BinaryFormatter();
fmt.Serialize(ms, ds);
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
// Deserialize
DataSet2 ds2 = (DataSet2)fmt.Deserialize(ms);
ms.Close();
// At the end the value of DateTime field in DataTable1 is 01/01/0001
01:00 (DateTime.MinValue) |