Discussion:
Error Deserializing DataSet Schema When Enum Column Uses Default Value
(too old to reply)
PeterWellington
2006-03-09 07:11:27 UTC
Permalink
I have a column in a data table that stores enum values and assigns a default
value:

Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek))
dc.DefaultValue = DayOfWeek.Thursday

When I try to serialize/deserialize dataset schema, I get the error below
during deserialization:

"System.ArgumentException: The DefaultValue for column TestEnumField is of
type System.DBNull, but the column is of type System.DayOfWeek."

I've tried reading/writing the XML using the dataset's built-in methods as
well as using the SOAP formatter, both with the same results.

When I look at the XML file that gets created during serialization, I can see
that the default value for the column is there. In the above case, it is
stored as an integer, 4. So, it's not DBNull but maybe there's some problem
during the deserialization process converting 4 into DayOfWeek type?

Is this a bug or am I doing something wrong?

Thanks.
Cor Ligthert [MVP]
2006-03-09 07:46:10 UTC
Permalink
Peter,

We cannot see if you do something wrong. How do you do the serializing. Is
it like this sample on our website?

http://www.vb-tips.com/default.aspx?ID=06d9730e-9e33-404c-947a-c891846eaf0b

Cor
Post by PeterWellington
I have a column in a data table that stores enum values and assigns a default
Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek))
dc.DefaultValue = DayOfWeek.Thursday
When I try to serialize/deserialize dataset schema, I get the error below
"System.ArgumentException: The DefaultValue for column TestEnumField is of
type System.DBNull, but the column is of type System.DayOfWeek."
I've tried reading/writing the XML using the dataset's built-in methods as
well as using the SOAP formatter, both with the same results.
When I look at the XML file that gets created during serialization, I can see
that the default value for the column is there. In the above case, it is
stored as an integer, 4. So, it's not DBNull but maybe there's some problem
during the deserialization process converting 4 into DayOfWeek type?
Is this a bug or am I doing something wrong?
Thanks.
PeterWellington
2006-03-09 08:26:49 UTC
Permalink
Post by Cor Ligthert [MVP]
Peter,
We cannot see if you do something wrong. How do you do the serializing. Is
it like this sample on our website?
http://www.vb-tips.com/default.aspx?ID=06d9730e-9e33-404c-947a-c891846eaf0b
Cor
Post by PeterWellington
I have a column in a data table that stores enum values and assigns a default
[quoted text clipped - 22 lines]
Post by PeterWellington
Thanks.
Yes, I've tried serializing/deserializing just as in the link you provided.
I'm pretty certain that's all correct in my code. For example, everything
serializes/deserializes error-free if I just take out the line of code where
I'm setting the default value for the DataColumn (i.e. commenting out "dc.
DefaultValue = DayOfWeek.Thursday").
Cor Ligthert [MVP]
2006-03-09 17:25:04 UTC
Permalink
Peter,

This code did run for me withouth any problem

\\\\
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField", GetType(DayOfWeek))
dt.LoadDataRow(New Object() {DayOfWeek.Thursday}, True)
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.ToString
///

I hope this helps,

Cor
Post by PeterWellington
I have a column in a data table that stores enum values and assigns a default
Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek))
dc.DefaultValue = DayOfWeek.Thursday
When I try to serialize/deserialize dataset schema, I get the error below
"System.ArgumentException: The DefaultValue for column TestEnumField is of
type System.DBNull, but the column is of type System.DayOfWeek."
I've tried reading/writing the XML using the dataset's built-in methods as
well as using the SOAP formatter, both with the same results.
When I look at the XML file that gets created during serialization, I can see
that the default value for the column is there. In the above case, it is
stored as an integer, 4. So, it's not DBNull but maybe there's some problem
during the deserialization process converting 4 into DayOfWeek type?
Is this a bug or am I doing something wrong?
Thanks.
PeterWellington via DotNetMonster.com
2006-03-10 03:45:38 UTC
Permalink
Post by Cor Ligthert [MVP]
Peter,
This code did run for me withouth any problem
\\\\
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField", GetType(DayOfWeek))
dt.LoadDataRow(New Object() {DayOfWeek.Thursday}, True)
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.ToString
///
I hope this helps,
Cor
Post by PeterWellington
I have a column in a data table that stores enum values and assigns a default
[quoted text clipped - 22 lines]
Post by PeterWellington
Thanks.
The problem is not writing it to a stream or file or reading from the file,
per se. It's during deserialization when a dataset is created from the
schema that was written to the XML file during the serialization process.
For example:

originalDataSet.WriteXmlSchema(filePath)
Dim targetDataSet As New DataSet()
targetDataSet.ReadXmlSchema(filePath)

The exception occurs when the target dataset tries to read back the XML
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-general/200603/1
Cor Ligthert [MVP]
2006-03-10 06:19:56 UTC
Permalink
Peter,

This code shows Thursday in a datagridview

Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField", GetType(DayOfWeek))
dt.LoadDataRow(New Object() {DayOfWeek.Thursday}, True)
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.ToString
Dim sr As New System.IO.StringReader(mystring)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ds2 As New DataSet
ds2.ReadXml(sr)
DataGridView1.DataSource = ds.Tables(0)
///

I can write the schema as well in this proces, that does not change the
result, however.

I hope this helps,

Cor
Post by PeterWellington via DotNetMonster.com
Post by Cor Ligthert [MVP]
Peter,
This code did run for me withouth any problem
\\\\
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField", GetType(DayOfWeek))
dt.LoadDataRow(New Object() {DayOfWeek.Thursday}, True)
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.ToString
///
I hope this helps,
Cor
Post by PeterWellington
I have a column in a data table that stores enum values and assigns a default
[quoted text clipped - 22 lines]
Post by PeterWellington
Thanks.
The problem is not writing it to a stream or file or reading from the file,
per se. It's during deserialization when a dataset is created from the
schema that was written to the XML file during the serialization process.
originalDataSet.WriteXmlSchema(filePath)
Dim targetDataSet As New DataSet()
targetDataSet.ReadXmlSchema(filePath)
The exception occurs when the target dataset tries to read back the XML
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-general/200603/1
PeterWellington via DotNetMonster.com
2006-03-10 07:57:32 UTC
Permalink
The problem occurs when you set a default value for the column. Here's a
simplified version of my code:

Dim ds As New DataSet("dsTest")
Dim dt As New DataTable("dtTest")

Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek))
dc.DefaultValue = DayOfWeek.Thursday
dt.Columns.Add(dc)
ds.Tables.Add(dt)

ds.WriteXmlSchema("C:\Temp\ds.xml")
Dim targetDS As New DataSet
targetDS.ReadXmlSchema("C:\Temp\ds.xml")

I think you'll find an error as well if you run the above code.
Post by Cor Ligthert [MVP]
Peter,
This code shows Thursday in a datagridview
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField", GetType(DayOfWeek))
dt.LoadDataRow(New Object() {DayOfWeek.Thursday}, True)
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.ToString
Dim sr As New System.IO.StringReader(mystring)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ds2 As New DataSet
ds2.ReadXml(sr)
DataGridView1.DataSource = ds.Tables(0)
///
I can write the schema as well in this proces, that does not change the
result, however.
I hope this helps,
Cor
Post by PeterWellington via DotNetMonster.com
Post by Cor Ligthert [MVP]
Peter,
[quoted text clipped - 34 lines]
Post by PeterWellington via DotNetMonster.com
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
--
Message posted via http://www.dotnetmonster.com
Cor Ligthert [MVP]
2006-03-10 10:41:43 UTC
Permalink
Peter,

You are right, even this won't work.

\\\
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField")
dt.Columns("TestEnumField").DataType = GetType(System.DayOfWeek)
dt.Columns(0).DefaultValue = DayOfWeek.Thursday
dt.Rows.Add(dt.NewRow)
ds.WriteXml("C:\test.xml", XmlWriteMode.WriteSchema)
Dim ds2 As New DataSet
ds2.ReadXml("C:\test.xml")
DataGridView1.DataSource = ds2.Tables(0)
///
He is converting the default value to 4 and won't than except it anymore.

I assume that it is a bug, I will see if (how) I can report this.

Cor
Post by PeterWellington via DotNetMonster.com
The problem occurs when you set a default value for the column. Here's a
Dim ds As New DataSet("dsTest")
Dim dt As New DataTable("dtTest")
Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek))
dc.DefaultValue = DayOfWeek.Thursday
dt.Columns.Add(dc)
ds.Tables.Add(dt)
ds.WriteXmlSchema("C:\Temp\ds.xml")
Dim targetDS As New DataSet
targetDS.ReadXmlSchema("C:\Temp\ds.xml")
I think you'll find an error as well if you run the above code.
Post by Cor Ligthert [MVP]
Peter,
This code shows Thursday in a datagridview
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField", GetType(DayOfWeek))
dt.LoadDataRow(New Object() {DayOfWeek.Thursday}, True)
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.ToString
Dim sr As New System.IO.StringReader(mystring)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ds2 As New DataSet
ds2.ReadXml(sr)
DataGridView1.DataSource = ds.Tables(0)
///
I can write the schema as well in this proces, that does not change the
result, however.
I hope this helps,
Cor
Post by PeterWellington via DotNetMonster.com
Post by Cor Ligthert [MVP]
Peter,
[quoted text clipped - 34 lines]
Post by PeterWellington via DotNetMonster.com
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
--
Message posted via http://www.dotnetmonster.com
Cor Ligthert [MVP]
2006-03-10 15:54:45 UTC
Permalink
Peter,

I have reported it as Bug

Cor
Post by Cor Ligthert [MVP]
Peter,
You are right, even this won't work.
\\\
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField")
dt.Columns("TestEnumField").DataType = GetType(System.DayOfWeek)
dt.Columns(0).DefaultValue = DayOfWeek.Thursday
dt.Rows.Add(dt.NewRow)
ds.WriteXml("C:\test.xml", XmlWriteMode.WriteSchema)
Dim ds2 As New DataSet
ds2.ReadXml("C:\test.xml")
DataGridView1.DataSource = ds2.Tables(0)
///
He is converting the default value to 4 and won't than except it anymore.
I assume that it is a bug, I will see if (how) I can report this.
Cor
Post by PeterWellington via DotNetMonster.com
The problem occurs when you set a default value for the column. Here's a
Dim ds As New DataSet("dsTest")
Dim dt As New DataTable("dtTest")
Dim dc As New DataColumn("TestEnumField", GetType(DayOfWeek))
dc.DefaultValue = DayOfWeek.Thursday
dt.Columns.Add(dc)
ds.Tables.Add(dt)
ds.WriteXmlSchema("C:\Temp\ds.xml")
Dim targetDS As New DataSet
targetDS.ReadXmlSchema("C:\Temp\ds.xml")
I think you'll find an error as well if you run the above code.
Post by Cor Ligthert [MVP]
Peter,
This code shows Thursday in a datagridview
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
dt.Columns.Add("TestEnumField", GetType(DayOfWeek))
dt.LoadDataRow(New Object() {DayOfWeek.Thursday}, True)
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.ToString
Dim sr As New System.IO.StringReader(mystring)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ds2 As New DataSet
ds2.ReadXml(sr)
DataGridView1.DataSource = ds.Tables(0)
///
I can write the schema as well in this proces, that does not change the
result, however.
I hope this helps,
Cor
Post by PeterWellington via DotNetMonster.com
Post by Cor Ligthert [MVP]
Peter,
[quoted text clipped - 34 lines]
Post by PeterWellington via DotNetMonster.com
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
--
Message posted via http://www.dotnetmonster.com
PeterWellington via DotNetMonster.com
2006-03-11 08:11:13 UTC
Permalink
Thanks for looking into this. I appreciate it.
Post by Cor Ligthert [MVP]
Peter,
I have reported it as Bug
Cor
Post by Cor Ligthert [MVP]
Peter,
[quoted text clipped - 67 lines]
Post by Cor Ligthert [MVP]
Post by PeterWellington via DotNetMonster.com
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-general/200603/1
Loading...