Dataset getchanges method

  • Thread starter Mustafa Korkmaz
  • Start date
M

Mustafa Korkmaz

I have a dataset which has the parent child relation set. If I add a
record to the detail table (BTW master table has a record and its row
state is unchanged) and get the changes from the dataset via
getchanges method, I get the detail record (expected) and master table
record (not expected)in the returned dataset. The funny thing I
noticed though, if you check the rowstate of the parent record, it's
still unchanged. What am I missing here?

Here is the xsd file of the dataset
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="dstFormDataType"
targetNamespace="http://tempuri.org/dstFormDataType.xsd"
elementFormDefault="qualified"
attributeFormDefault="qualified"
xmlns="http://tempuri.org/dstFormDataType.xsd"
xmlns:mstns="http://tempuri.org/dstFormDataType.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="dstFormDataType" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Table1">
<xs:complexType>
<xs:sequence>
<xs:element name="Field1" type="xs:string" minOccurs="0" />
<xs:element name="Field2" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Table2">
<xs:complexType>
<xs:sequence>
<xs:element name="Field1" type="xs:string" minOccurs="0" />
<xs:element name="Field3" type="xs:string" minOccurs="0" />
<xs:element name="Field4" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="dstFormDataTypeKey1" msdata:primaryKey="true">
<xs:selector xpath=".//mstns:Table1" />
<xs:field xpath="mstns:Field1" />
</xs:key>
<xs:key name="dstFormDataTypeKey2" msdata:primaryKey="true">
<xs:selector xpath=".//mstns:Table2" />
<xs:field xpath="mstns:Field1" />
<xs:field xpath="mstns:Field3" />
</xs:key>
<xs:keyref name="Table1Table2" refer="dstFormDataTypeKey1"
msdata:AcceptRejectRule="Cascade"
msdata:DeleteRule="Cascade" msdata:UpdateRule="Cascade">
<xs:selector xpath=".//mstns:Table2" />
<xs:field xpath="mstns:Field1" />
</xs:keyref>
</xs:element>
</xs:schema>

Here is the sample project in vb.net to test.
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DstFormData As RowStateTest.dstFormDataType
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.DstFormData = New RowStateTest.dstFormDataType
Me.Button1 = New System.Windows.Forms.Button
CType(Me.DstFormData,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DstFormData
'
Me.DstFormData.DataSetName = "dstFormData"
Me.DstFormData.Locale = New
System.Globalization.CultureInfo("en-US")
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(88, 68)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DstFormData,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim drwnew As DataRow = DstFormData.Tables(0).NewRow
drwnew("Field1") = "1"
DstFormData.Tables(0).Rows.Add(drwnew)
DstFormData.AcceptChanges()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim drwnew As DataRow = DstFormData.Tables(1).NewRow
drwnew("Field1") = "1"
drwnew("Field3") = "2"
DstFormData.Tables(1).Rows.Add(drwnew)
MessageBox.Show(DstFormData.GetChanges.GetXml)
End Sub
End Class

regards,
mk
 
M

Marina

I guess I"m not sure why you are confused. The child record is the record
that changed, not the parent.

Just because there is a foreign key between child and parent, that doesn't
make them part of one entity. They are separate entities. If only the
child record changed, then I wouldn't expect the parent to be flagged as
changed. Because then you can ask: what is the change in the parent? And
the answer would be 'nothing', because nothing has changed in the parent. So
then how can it be flagged as changed? It can't. The data adapter
wouldn't be able to update it even - because it says it has changed, but all
the values are exactly as they were before.
 

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