LoadDataRow Issues

V

VJ

I have 2 datasets, each with a DataTable. Each table has the same primary
key, and columns. I am trying to copy rows from one dataset to another,
based on an event from my application.

It all works fine when the row is not present in the destination DataTable.
Now when the row is present in the destination file, the .NET documentation
says use the LoadRowData method of the dataset to copy the row. When do
this, I get a unique key violation problem (right at the EndLoadData point
in the code below).. Here is my code below. Can anybody help??


Dim app As Application
Dim xmlDestFile As String = app.StartupPath & "\" & "dest.xml"
Dim xmlSourceFile As String = app.StartupPath & "\" & "source.xml"
Dim dsDest As New DataSet
Dim dsSource As New DataSet
Dim rwDest As DataRow
Dim rwSource As DataRow

dsDest.ReadXml(xmlDestFile)
dsSource.ReadXml(xmlDestFile)

Try
For Each rwSource In dsSource.Tables(0).Rows
Dim copyRowPersent As Boolean

For Each rwDest In dsDest.Tables(0).Rows
If rwDest.Item("Name") = rwSource.Item("Name") Then
copyRowPersent = True
Exit For
End If
Next

If copyRowPersent Then

Dim newRow(rwSource.Table.Columns.Count - 1) As Object
Dim column As DataColumn
Dim intLoop As Int32

intLoop = 0

For Each column In rwSource.Table.Columns
newRow(intLoop) = rwSource.Item(column)
intLoop = intLoop + 1
Next

dsDest.Tables(0).BeginLoadData()
dsDest.Tables(0).LoadDataRow(newRow, False)
dsDest.Tables(0).EndLoadData() ' I get a
primary key voilation problem here..., I don't even go to accept changes

else
'Import the new rows here using ImportRow
' This works fine...

End If

Next

dsDest.AcceptChanges()
dsDest.WriteXml(xmlDestFile, XmlWriteMode.WriteSchema)
dsDest.Dispose()
dsSource.Dispose()

Catch myex As Exception

MsgBox(myex.ToString, MsgBoxStyle.Information +
MsgBoxStyle.OKOnly, "Test")

End Try


VJ
 
J

Josh Moody [MSFT]

According to help, EndLoadData does the following:
Turns on notifications, index maintenance, and constraints after
loading data.
Since it enables the constraints, any unique key constraints will be
enabled, and thus an exception gets thrown because you have two rows with
the same unique key.

Depending on your situation, you will either have to update your unique
key, or update the row that the prior unique key was on.

Josh Moody
VSU Team

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
 
V

VJ

I have 2 datasets, each with a DataTable. Each table has the same primary
key, and columns. I am trying to copy rows from one dataset to another,
based on an event from my application.

It all works fine when the row is not present in the destination DataTable.
Now when the row is present in the destination file, the .NET documentation
says use the LoadRowData method of the dataset to copy the row. When do
this, I get a unique key violation problem (right at the EndLoadData point
in the code below).. Here is my code below. Can anybody help??


Dim app As Application
Dim xmlDestFile As String = app.StartupPath & "\" & "dest.xml"
Dim xmlSourceFile As String = app.StartupPath & "\" & "source.xml"
Dim dsDest As New DataSet
Dim dsSource As New DataSet
Dim rwDest As DataRow
Dim rwSource As DataRow

dsDest.ReadXml(xmlDestFile)
dsSource.ReadXml(xmlDestFile)

Try
For Each rwSource In dsSource.Tables(0).Rows
Dim copyRowPersent As Boolean

For Each rwDest In dsDest.Tables(0).Rows
If rwDest.Item("Name") = rwSource.Item("Name") Then
copyRowPersent = True
Exit For
End If
Next

If copyRowPersent Then

rwDest.delete
rwDest.Tables(0).ImportRow(rwSource)

' Dim newRow(rwSource.Table.Columns.Count - 1) As Object
' Dim column As DataColumn
' Dim intLoop As Int32

' intLoop = 0

' For Each column In rwSource.Table.Columns
' newRow(intLoop) = rwSource.Item(column)
' intLoop = intLoop + 1
' Next

' dsDest.Tables(0).BeginLoadData()
' dsDest.Tables(0).LoadDataRow(newRow, False)
' dsDest.Tables(0).EndLoadData() ' I get a
primary key voilation problem here..., I don't even go to accept changes

else
'Import the new rows here using ImportRow
' This works fine...

End If

Next

dsDest.AcceptChanges()
dsDest.WriteXml(xmlDestFile, XmlWriteMode.WriteSchema)
dsDest.Dispose()
dsSource.Dispose()

Catch myex As Exception

MsgBox(myex.ToString, MsgBoxStyle.Information +
MsgBoxStyle.OKOnly, "Test")

End Try


VJ
 
V

VJ

so LoadDataRow is not updating the row I am giving. It is rather inserting
it.. The documentation says it will update when i am trying to gave the same
row... Why is it happening?

VJ
 

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