microsoft.public.hk.visualstudioanddevelopment
[Top] [All Lists]

Re: Serializing and Deserializing a DataTable that contains a user defin

Subject: Re: Serializing and Deserializing a DataTable that contains a user define struct
From: "milke.chen"
Date: Mon, 18 Feb 2008 23:09:47 +0800
Newsgroups: microsoft.public.hk.visualstudioanddevelopment
Datatable serializing doesn't support custom type column. If you add system 
default typed column, it could be serialized without problem, even column 
name contains space.

On the side of issue, if you create a new Period instance, you can serizlize 
it without issue. ISerializable.GetObjectData method and 
Period(SerializationInfo info, StreamingContext context) contructor will be 
called correctly. But, with Datatable, these method could not work.

Thanks


"Kwokho" <kwokho74@xxxxxxxxxxx> wrote in message 
news:eiIBGu5bIHA.4968@xxxxxxxxxxxxxxxxxxxxxxx
> Dear all,
>
> I have 2 problems on Serializing and Deserializing a DataTable.
>
> I have a struct named "Period", then I create DataColumn with this data 
> type
> and add it to the DataTable. There are 2 issues (please read the 
> attachments
> for detail).
>
> First issue:
>        private static void Test1()
>        {
>            DataTable dt = new DataTable();
>            dt.Columns.Add("c1", typeof(Period));
>            dt.Columns.Add("c 2", typeof(Period)); //column name contains
> space causes unexcepted result
>            Period p1 = new Period(2008, 1);
>            Period p2 = new Period(2008, 1);
>            dt.Rows.Add(p1, p2);
>
>            byte[] bb = Serialize(dt);
>
>            DataTable dt2 = (DataTable)Deserialize(bb);
>            object o1 = dt2.Rows[0][0]; //expected result
>            object o2 = dt2.Rows[0][1]; //unexpected result(System.DBNull)
>        }
>
> in the code above, the second column name contains a space, after
> deserialize, the data in the second column contains DBNull.
>
>
> Second issue:
>        private static void Test2()
>        {
>            DataTable dt = new DataTable();
>            dt.Columns.Add("c1", typeof(Period));
>            dt.Columns[0].DefaultValue = new Period(2001, 1);
>            Period p1 = new Period(2008, 1);
>            dt.Rows.Add(p1);
>
>            //"ArgumentException" is thrown when serialize the data table.
>            //"DataColumn with type 'ConsoleApplication1.Period,
> ConsoleApplication1, Version=1.0.0.0, Culture=neutral,
>            // PublicKeyToken=null' is a complexType. Can not serialize
> value of a complex type as Attribute"
>            byte[] bb = Serialize(dt);
>
>            DataTable dt2 = (DataTable)Deserialize(bb);
>            object o1 = dt2.Rows[0][0];
>        }
>
> in this case the DataTable cannot be deserialized, it thown an
> ArgumentException. I'm not sure why this issue happen.
> I have implement the ISerializable.GetObjectData(SerializationInfo info,
> StreamingContext context) method and also included
> a Period(SerializationInfo info, StreamingContext context) contructor. But 
> I
> found that it didn't step into these 2 methods.
>
> Also, I would like to know, in which case of serialization and
> deserialization, it will step into the
> ISerializable.GetObjectData(SerializationInfo info, StreamingContext
> context) methood and Period(SerializationInfo info, StreamingContext
> context) contructor and which cases are not.
>
> Thanks a lot.
>
> Kwokho, FUNG
>
>
>
>
>
> 



<Prev in Thread] Current Thread [Next in Thread>