Binary Serialization of DataSets - bug with DateTime-Columns?

I just tried to serialize a datatable which contains two DateTime-Columns (I use .Net 2.0).
The result of serialize and deserialize a dataset in case if a DateTime-Column has value DBNull is not the same if you use binary- instead of xml-serialization!

Here is the result if you use XML-Serialization:
**** before serialize ****
Date1:
DAte2:  11/9/2005 3:03:01 PM
-------------------------------
**** after deserialize ****
Date1:
DAte2:  11/9/2005 3:03:01 PM

Here with Binary-Serialization:
**** before serialize ****
Date1:
DAte2:  11/9/2005 3:04:02 PM
-------------------------------
**** after deserialize ****
Date1:  1/1/0001 1:00:00 AM
DAte2:  11/9/2005 3:04:02 PM


In the second sample "date1" has value "1/1/0001 1:00:00 AM" instead of DBNull.

regards,
johannes


Here is the example (just copy it into a C#-Console-Application):

DataSet ds = new DataSet();

          DataSet ds = new DataSet();
            // create datatable with two colums (datetime, string)
            System.Data.DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("date1", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("date2", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("value1", typeof(System.String)));

            // create row and set column 'date1' to DBnull!!
            DataRow newRow = dt.NewRow();
            newRow["date1"] = System.DBNull.Value;
            newRow["date2"] = DateTime.Now;
            newRow["value1"] = "test";
            dt.Rows.Add(newRow);
           
            // add table to dataset
            ds.Tables.Add(dt);

            Console.WriteLine("**** before serialize ****\n");
            foreach (DataRow row in ds.Tables[0].Rows) {
                Console.WriteLine("Date1:  {0}", row["date1"]);
                Console.WriteLine("DAte2:  {0}", row["date2"]);
            }

            // Serialize dataset to MemoryStream
            // Result with SerializationFormat.Binary is different if you use
SerializationFormat.XML!!!
            ds.RemotingFormat = SerializationFormat.Binary;
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();

            binFormatter.Serialize(memStream, ds);
            memStream.Position =0;

            // Deserialize dataset
            ds = (DataSet) binFormatter.Deserialize(memStream);

            Console.WriteLine("-------------------------------");
            Console.WriteLine("**** after deserialize ****\n");
            foreach (DataRow row in ds.Tables[0].Rows) {
                Console.WriteLine("Date1:  {0}", row["date1"]);
                Console.WriteLine("DAte2:  {0}", row["date2"]);
            }
            Console.ReadKey(true);

 



Answer this question

Binary Serialization of DataSets - bug with DateTime-Columns?

  • Dlimanov

    We’ve solved this problem by applying the following hot fix at http://support.microsoft.com/default.aspx scid=kb;en-us;913766&sd=rss&spid=8291

    Regards

    Egbert


  • anonymizer

    I am also getting the same error when I use SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema even after setting the DateTimeMode to Unspecified.
  • Chris White

    Tongue Tied I am unable to set the DateTimeMode of DateTime columns in typed datasets. Even though it shows in the DataSet designer UI, it is not reflected in the generated dataset class. Is this a bug and is there a fix available. I am using VS2005 RC.
  • resyuz

    Hi Ravinder,

    thanks for the explanation of the bug.

    best regards,
    Johannes

  • John Wilkie

    This is one of the bugs which was reported very late in the cycle.

    The bug repros if the timezone setting happens to be ahead of GMT timezone mode [ex: Asia, Australia, some countries of Europe] and the datetime mode setting is set to backward-compatible default mode. [UnspecifiedLocal]

    There is a possible workaround for this bug:

    Set the datetime mode to one of the new modes: For ex, set it to Unspecified - [meaning the datetime will be remoted without any timeshift across timezone], it should work without any issues:

    do
    ---
    dt.Columns["date1"].DateTimeMode= DataSetDateTime.Unspecified;
    -----

    We will be fixing this bug in the next available service pack.

    HTH,
    Ravinder.

  • Lyric8

    Have this service been released If so when I'm suffering a lot from this bug :(


  • RandyTheWebGuy

    This bug will be fixed by Microsoft in the next service release (accoring post of RavInder).
    Do anyone have an idea when MS plan to provide this service release


  • Shiqin Xing

    Does anyone know whether this problem has been solved by now

    I’m using VS 2005 with .NET Framework V2.0.50727, and I still encounter this problem.

    Thanks

    Egbert


  • Binary Serialization of DataSets - bug with DateTime-Columns?