Adding records to an existing OleDb datatable

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Can someone explain why the following code works to add records to an OleDb
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

Dim intRow, intSql As Integer
Dim cb As New OleDb.OleDbCommandBuilder(dbAdapterSQL)
Dim dsNewRow As DataRow


Do Until intRow = dbDataSetPer.Tables(0).Rows.Count - 1
intSql = dbDataSetSql.Tables("SQLOutputs").Rows.Count

dsNewRow = dbDataSetSql.Tables("SQLOutputs").NewRow()
dsNewRow.Item("RepairNum") = dbDataSetPer.Tables(0).Rows(intRow).Item("Doc")
dsNewRow.Item("Line") = dbDataSetPer.Tables(0).Rows(intRow).Item("Line")
dsNewRow.Item("ItemNum") =dbDataSetPer.Tables(0).Rows(intRow).Item("RepairItemNum")
dsNewRow.Item("ItemDesc") =dbDataSetPer.Tables(0).Rows(intRow).Item("ItemDescription")
dsNewRow.Item("Pieces") = dbDataSetPer.Tables(0).Rows(intRow).Item("Pieces")
dsNewRow.Item("UCost") = dbDataSetPer.Tables(0).Rows(intRow).Item("UCost")
dsNewRow.Item("ExtCost") = dbDataSetPer.Tables(0).Rows(intRow).Item("ExtCost")
dsNewRow.Item("UPrice") = dbDataSetPer.Tables(0).Rows(intRow).Item("UPrice")
dsNewRow.Item("ExtPrice") = dbDataSetPer.Tables(0).Rows(intRow).Item("ExtPrice")

dbDataSetSql.Tables("SQLOutputs").Rows.Add(dsNewRow)
dbAdapterSQL.Update(dbDataSetSql, "SQLOutputs")
intRow += 1
Loop

dgSql.Refresh()

End Sub
And why with the following code I receive this error message: "Update requires a valid InsertCommand when passed DataRow collection with new rows."
Public Sub FillBenchRepairDetail(ByVal tblInput As DataTable)


Dim cb As New OleDb.OleDbCommandBuilder(dtAddAdapter)
Dim qryAdd As String
Dim intA As Integer
Dim nw As DataRow

dtAddcon.ConnectionString = sConStrSQL

qryAdd = "Select " _
& "[RepairNum] " _
& ",[ItemNum] " _
& ",[SubClass] " _
& ",[ItemDescription] " _
& ",[LineNum] " _
& ",[SDate] " _
& ",[STime] " _
& ",[TDate] " _
& ",[Qty] " _
& ",[ExtCost] " _
& ",[ExtPrice] " _
& "From [Repairs].[dbo].[BenchRepairDetail]"

dtAddAdapter = New OleDbDataAdapter(qryAdd, dtAddcon)
dtAddAdapter.Fill(dtAdd, "tblAdd")

For intA = 0 To tblInput.Rows.Count - 1

nw = dtAdd.Tables("tblAdd").NewRow()

nw.Item(0) = tblInput.Rows(intA).Item(0)
nw.Item(1) = tblInput.Rows(intA).Item(1)
nw.Item(2) = tblInput.Rows(intA).Item(2)
nw.Item(3) = tblInput.Rows(intA).Item(3)
nw.Item(4) = tblInput.Rows(intA).Item(4)
nw.Item(5) = tblInput.Rows(intA).Item(5)
nw.Item(6) = tblInput.Rows(intA).Item(6)
nw.Item(7) = tblInput.Rows(intA).Item(7)
nw.Item(8) = tblInput.Rows(intA).Item(8)
nw.Item(9) = tblInput.Rows(intA).Item(9)
nw.Item(10) = tblInput.Rows(intA).Item(10)

dtAdd.Tables("tblAdd").Rows.Add(nw)
dtAddAdapter.Update(dtAdd, "tblAdd")

Next

End Sub
Both sets of code are writing to the same SQL 2008 R2 database?
Thank you in advance for your help
Dean

View the full article
 
Back
Top