whiwex
asked on
Row Already belongs to this table.
I have a page with a button that when clicked this code fires. All I am trying to do is add all the rows from a datatable to a access database.
I can add one new row but when I add the second new row I get the error row already belongs to this table
Here's the code:
Dim row As Integer
Dim dtCertificates As New DataTable
Dim TempCerts As DataTable
Dim drNewRow2 As DataRow = dtCertificates.NewRow
cn1ADOconnection.Connectio nString = "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source= C:\Inetpub\wwwstalsmanna\D atabases\i nventory.M DB"
cn1ADOconnection.Open()
daData1Adapter = New OleDb.OleDbDataAdapter("Se lect * From [inventory] where [VendorName] = '" & ddlVendors.Text & "' ", cn1ADOconnection)
cb1CommandBuilder = New OleDb.OleDbCommandBuilder( daData1Ada pter)
daData1Adapter.Fill(dtCert ificates)
dtCertificates.Clear()
'Add the certificates to inventory
row = 0
TempCerts = CType(Session("NewInventor y"), DataTable)
While row < TempCerts.Rows.Count
With drNewRow2
drNewRow2("VendorName") = TempCerts.Rows(row)(0)
drNewRow2("certificatenumb er") = TempCerts.Rows(row)(3)
drNewRow2("CertificateValu e") = Val(TempCerts.Rows(row)(1) )
drNewRow2("PurchaseDate") = txtPurchaseDate.Text
drNewRow2("entereddate") = Today
drNewRow2("Certificatetype ") = TempCerts.Rows(row)(2)
drNewRow2("EnteredBy") = User.Identity.Name
End With
dtCertificates.Rows.Add(dr NewRow2)
daData1Adapter.Update(dtCe rtificates )
row = row + 1
End While
I can add one new row but when I add the second new row I get the error row already belongs to this table
Here's the code:
Dim row As Integer
Dim dtCertificates As New DataTable
Dim TempCerts As DataTable
Dim drNewRow2 As DataRow = dtCertificates.NewRow
cn1ADOconnection.Connectio
cn1ADOconnection.Open()
daData1Adapter = New OleDb.OleDbDataAdapter("Se
cb1CommandBuilder = New OleDb.OleDbCommandBuilder(
daData1Adapter.Fill(dtCert
dtCertificates.Clear()
'Add the certificates to inventory
row = 0
TempCerts = CType(Session("NewInventor
While row < TempCerts.Rows.Count
With drNewRow2
drNewRow2("VendorName") = TempCerts.Rows(row)(0)
drNewRow2("certificatenumb
drNewRow2("CertificateValu
drNewRow2("PurchaseDate") = txtPurchaseDate.Text
drNewRow2("entereddate") = Today
drNewRow2("Certificatetype
drNewRow2("EnteredBy") = User.Identity.Name
End With
dtCertificates.Rows.Add(dr
daData1Adapter.Update(dtCe
row = row + 1
End While
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That was it.