Error Deserializing DataSet Schema When Enum Column Uses Default Value

  • Thread starter PeterWellington
  • Start date
P

PeterWellington

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.
 
P

PeterWellington

Cor said:
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
I have a column in a data table that stores enum values and assigns a
default
[quoted text clipped - 22 lines]

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").
 
C

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
 
P

PeterWellington via DotNetMonster.com

Cor said:
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
I have a column in a data table that stores enum values and assigns a
default
[quoted text clipped - 22 lines]

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.
 
C

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

PeterWellington via DotNetMonster.com said:
Cor said:
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
I have a column in a data table that stores enum values and assigns a
default
[quoted text clipped - 22 lines]

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.
 
P

PeterWellington via DotNetMonster.com

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.
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
[quoted text clipped - 34 lines]
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
 
C

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


PeterWellington via DotNetMonster.com said:
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.
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
[quoted text clipped - 34 lines]
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
 
C

Cor Ligthert [MVP]

Peter,

I have reported it as Bug

Cor


Cor Ligthert said:
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


PeterWellington via DotNetMonster.com said:
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.
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

Peter,

[quoted text clipped - 34 lines]
schema. As I mentioned before, I could also use a SOAP formatter to
serialize/deserialize and I get the same error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top