G
Guest
(Above there is the whole code.)
I have a dataset with 2 tables (father and child table) and a
relation between them with cascade for update and delete.
On this DataSet I delete a existing father's row and afterwards create the
same father's row with the same field values and also create a child row.
Afterwards, I execute the serialization of this DataSet to a file and, and
the deserialization to another DataSet. You will see that the deserialized
DataSet doesn't has the chid row (it was vanished).
But note that the child row has vanished only if you create the father's
row with the same field's value from the deleted father's row.
This is a terrible problem. How can we use this technology in a serious
application?
Can someone help me?
*** The code ***
It is necessary to create a Form (Form1), copy the variable declarations and
copy the statements inside the Form1_Load().
Thanks.
Mauricio Pires
ControlBase (Brazil)
'Variable declarations"
Friend FatherTable As New DataTable("FatherTable")
Friend ChildTable As New DataTable("ChildTable")
Friend dsOriginal As New DataSet
Friend dsRead As DataSet
Friend WithEvents DataGridOriginalFather As System.Windows.Forms.DataGrid
Friend WithEvents DataGridOriginalChild As System.Windows.Forms.DataGrid
'Form_Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ClientSize = New System.Drawing.Size(600, 266)
'Creating the DataGrids
Me.DataGridOriginalFather = New System.Windows.Forms.DataGrid
Me.DataGridOriginalFather.CaptionText = "Original Father"
Me.DataGridOriginalFather.Location = New System.Drawing.Point(56, 24)
Me.DataGridOriginalFather.Name = "DataGridOriginalFather"
Me.DataGridOriginalFather.Size = New System.Drawing.Size(184, 112)
Me.DataGridOriginalChild = New System.Windows.Forms.DataGrid
Me.DataGridOriginalChild.CaptionText = "Original Child"
Me.DataGridOriginalChild.Location = New System.Drawing.Point(40, 168)
Me.DataGridOriginalChild.Name = "DataGridOriginalChild"
Me.DataGridOriginalChild.Size = New System.Drawing.Size(232, 80)
Me.DataGridDeserializedFather = New System.Windows.Forms.DataGrid
Me.DataGridDeserializedFather.CaptionText = "Deserialized Father"
Me.DataGridDeserializedFather.Location = New
System.Drawing.Point(344, 24)
Me.DataGridDeserializedFather.Name = "DataGridDeserializedFather"
Me.DataGridDeserializedFather.Size = New System.Drawing.Size(184, 120)
Me.DataGridDeserializedChild = New System.Windows.Forms.DataGrid
Me.DataGridDeserializedChild.CaptionText = "Deserialized Child (the
child vanished)"
Me.DataGridDeserializedChild.HeaderForeColor =
System.Drawing.SystemColors.ControlText
Me.DataGridDeserializedChild.Location = New
System.Drawing.Point(328, 168)
Me.DataGridDeserializedChild.Name = "DataGridDeserializedChild"
Me.DataGridDeserializedChild.Size = New System.Drawing.Size(232, 80)
Me.Controls.Add(Me.DataGridOriginalChild)
Me.Controls.Add(Me.DataGridOriginalFather)
Me.Controls.Add(Me.DataGridDeserializedFather)
Me.Controls.Add(Me.DataGridDeserializedChild)
'Creating 2 tables (father and child tables) and a relation
Dim fatherCol1 As New DataColumn("FatherCol1")
Dim childCol1 As New DataColumn("ChildCol1")
Dim childCol2 As New DataColumn("ChildCol2")
FatherTable.Columns.Add(fatherCol1)
ChildTable.Columns.Add(childCol1)
ChildTable.Columns.Add(childCol2)
dsOriginal.Tables.Add(FatherTable)
dsOriginal.Tables.Add(ChildTable)
dsOriginal.Relations.Add("FatherChildRelation", fatherCol1, childCol1)
'Creating a father row
FatherTable.Rows.Add(New Object() {"Father1"})
Me.dsOriginal.AcceptChanges()
'Deleting the father row that was created above
FatherTable.Rows(0).Delete()
'Creating the same father row again
FatherTable.Rows.Add(New Object() {"Father1"})
'Creating a child row
ChildTable.Rows.Add(New Object() {"Father1", "Child1"})
'Serialization to "myFile.xml"
Dim mySerializer As New
System.Xml.Serialization.XmlSerializer(GetType(DataSet))
Dim myWriter As IO.StreamWriter = New
IO.StreamWriter("myFileName.xml")
mySerializer.Serialize(myWriter, dsOriginal)
myWriter.Close()
'Deserialization (see that dataset lost the child row)
Dim myFileStream As IO.FileStream = New
IO.FileStream("myFileName.xml", IO.FileMode.Open)
' Calls the Deserialize.
dsRead = CType( _
mySerializer.Deserialize(myFileStream), DataSet)
myFileStream.Close()
'This DataDrid show the DataSet with one father and one child rows
(OK)
Me.DataGridOriginalFather.SetDataBinding(dsOriginal, "FatherTable")
Me.DataGridOriginalChild.SetDataBinding(dsOriginal,
"FatherTable.FatherChildRelation")
'These DataGrids show the dataset with one father and anyone chid
row (The child vanished)
Me.DataGridDeserializedFather.SetDataBinding(Me.dsRead, "FatherTable")
Me.DataGridDeserializedChild.SetDataBinding(Me.dsRead,
"FatherTable.FatherChildRelation")
End Sub
'------ THE END -------------
I have a dataset with 2 tables (father and child table) and a
relation between them with cascade for update and delete.
On this DataSet I delete a existing father's row and afterwards create the
same father's row with the same field values and also create a child row.
Afterwards, I execute the serialization of this DataSet to a file and, and
the deserialization to another DataSet. You will see that the deserialized
DataSet doesn't has the chid row (it was vanished).
But note that the child row has vanished only if you create the father's
row with the same field's value from the deleted father's row.
This is a terrible problem. How can we use this technology in a serious
application?
Can someone help me?
*** The code ***
It is necessary to create a Form (Form1), copy the variable declarations and
copy the statements inside the Form1_Load().
Thanks.
Mauricio Pires
ControlBase (Brazil)
'Variable declarations"
Friend FatherTable As New DataTable("FatherTable")
Friend ChildTable As New DataTable("ChildTable")
Friend dsOriginal As New DataSet
Friend dsRead As DataSet
Friend WithEvents DataGridOriginalFather As System.Windows.Forms.DataGrid
Friend WithEvents DataGridOriginalChild As System.Windows.Forms.DataGrid
'Form_Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ClientSize = New System.Drawing.Size(600, 266)
'Creating the DataGrids
Me.DataGridOriginalFather = New System.Windows.Forms.DataGrid
Me.DataGridOriginalFather.CaptionText = "Original Father"
Me.DataGridOriginalFather.Location = New System.Drawing.Point(56, 24)
Me.DataGridOriginalFather.Name = "DataGridOriginalFather"
Me.DataGridOriginalFather.Size = New System.Drawing.Size(184, 112)
Me.DataGridOriginalChild = New System.Windows.Forms.DataGrid
Me.DataGridOriginalChild.CaptionText = "Original Child"
Me.DataGridOriginalChild.Location = New System.Drawing.Point(40, 168)
Me.DataGridOriginalChild.Name = "DataGridOriginalChild"
Me.DataGridOriginalChild.Size = New System.Drawing.Size(232, 80)
Me.DataGridDeserializedFather = New System.Windows.Forms.DataGrid
Me.DataGridDeserializedFather.CaptionText = "Deserialized Father"
Me.DataGridDeserializedFather.Location = New
System.Drawing.Point(344, 24)
Me.DataGridDeserializedFather.Name = "DataGridDeserializedFather"
Me.DataGridDeserializedFather.Size = New System.Drawing.Size(184, 120)
Me.DataGridDeserializedChild = New System.Windows.Forms.DataGrid
Me.DataGridDeserializedChild.CaptionText = "Deserialized Child (the
child vanished)"
Me.DataGridDeserializedChild.HeaderForeColor =
System.Drawing.SystemColors.ControlText
Me.DataGridDeserializedChild.Location = New
System.Drawing.Point(328, 168)
Me.DataGridDeserializedChild.Name = "DataGridDeserializedChild"
Me.DataGridDeserializedChild.Size = New System.Drawing.Size(232, 80)
Me.Controls.Add(Me.DataGridOriginalChild)
Me.Controls.Add(Me.DataGridOriginalFather)
Me.Controls.Add(Me.DataGridDeserializedFather)
Me.Controls.Add(Me.DataGridDeserializedChild)
'Creating 2 tables (father and child tables) and a relation
Dim fatherCol1 As New DataColumn("FatherCol1")
Dim childCol1 As New DataColumn("ChildCol1")
Dim childCol2 As New DataColumn("ChildCol2")
FatherTable.Columns.Add(fatherCol1)
ChildTable.Columns.Add(childCol1)
ChildTable.Columns.Add(childCol2)
dsOriginal.Tables.Add(FatherTable)
dsOriginal.Tables.Add(ChildTable)
dsOriginal.Relations.Add("FatherChildRelation", fatherCol1, childCol1)
'Creating a father row
FatherTable.Rows.Add(New Object() {"Father1"})
Me.dsOriginal.AcceptChanges()
'Deleting the father row that was created above
FatherTable.Rows(0).Delete()
'Creating the same father row again
FatherTable.Rows.Add(New Object() {"Father1"})
'Creating a child row
ChildTable.Rows.Add(New Object() {"Father1", "Child1"})
'Serialization to "myFile.xml"
Dim mySerializer As New
System.Xml.Serialization.XmlSerializer(GetType(DataSet))
Dim myWriter As IO.StreamWriter = New
IO.StreamWriter("myFileName.xml")
mySerializer.Serialize(myWriter, dsOriginal)
myWriter.Close()
'Deserialization (see that dataset lost the child row)
Dim myFileStream As IO.FileStream = New
IO.FileStream("myFileName.xml", IO.FileMode.Open)
' Calls the Deserialize.
dsRead = CType( _
mySerializer.Deserialize(myFileStream), DataSet)
myFileStream.Close()
'This DataDrid show the DataSet with one father and one child rows
(OK)
Me.DataGridOriginalFather.SetDataBinding(dsOriginal, "FatherTable")
Me.DataGridOriginalChild.SetDataBinding(dsOriginal,
"FatherTable.FatherChildRelation")
'These DataGrids show the dataset with one father and anyone chid
row (The child vanished)
Me.DataGridDeserializedFather.SetDataBinding(Me.dsRead, "FatherTable")
Me.DataGridDeserializedChild.SetDataBinding(Me.dsRead,
"FatherTable.FatherChildRelation")
End Sub
'------ THE END -------------