By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,462 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,462 IT Pros & Developers. It's quick & easy.

LoadDataRow issues

P: n/a
VJ
Hi..

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 copyRowNotPersent As Boolean

For Each rwDest In dsDest.Tables(0).Rows

If rwDest.Item("Name") = rwSource.Item("Name") Then

copyRowNotPersent = True

Exit For

End If

Next

If Not copyRowNotPersent Then

Else

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()

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
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Cor
Hi VJ,

First a qeustion, will you when you send again some code first copy it in
your notepad, copy it from there and then paste it in the message, now we
get code with 5 rows spaces in every row and have first to do your job.

I did not look to deep in the code, because there is something (I did put it
on the line below) you maybe did not see.

I hope it was it?

Cor
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)
Here you are reading the same xml file in both datasets, so the rows will
always be the same and if I look in a glance at your code "copyRowNotPersent
is always true" and nothing will be copied.
Try
For Each rwSource In dsSource.Tables(0).Rows
Dim copyRowNotPersent As Boolean
For Each rwDest In dsDest.Tables(0).Rows
If rwDest.Item("Name") = rwSource.Item("Name") Then
copyRowNotPersent = True
Exit For
End If
Next
If Not copyRowNotPersent Then
Else
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()
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

Nov 20 '05 #2

P: n/a
VJ
Maybe my variable declaration was not right... yes when the rows are present
the value will be set to true, which is what I except. When it is set to
true, I want to overwrite the values coming from the destination file..
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...

else
'Import the new rows here using ImportRow

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
and sorry about that code not being formatted... I usually do it... but it
was late in the night and I missed doing it...

Hope somebody can still help with my problem...

VJ

"Cor" <no*@non.com> wrote in message
news:ue****************@TK2MSFTNGP09.phx.gbl...
Hi VJ,

First a qeustion, will you when you send again some code first copy it in
your notepad, copy it from there and then paste it in the message, now we
get code with 5 rows spaces in every row and have first to do your job.

I did not look to deep in the code, because there is something (I did put it on the line below) you maybe did not see.

I hope it was it?

Cor
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)
Here you are reading the same xml file in both datasets, so the rows will
always be the same and if I look in a glance at your code

"copyRowNotPersent is always true" and nothing will be copied.
Try
For Each rwSource In dsSource.Tables(0).Rows
Dim copyRowNotPersent As Boolean
For Each rwDest In dsDest.Tables(0).Rows
If rwDest.Item("Name") = rwSource.Item("Name") Then
copyRowNotPersent = True
Exit For
End If
Next
If Not copyRowNotPersent Then
Else
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()
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


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.