Link to home
Start Free TrialLog in
Avatar of whiwex
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.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Inetpub\wwwstalsmanna\Databases\inventory.MDB"
        cn1ADOconnection.Open()
        daData1Adapter = New OleDb.OleDbDataAdapter("Select * From  [inventory]  where [VendorName] =   '" & ddlVendors.Text & "'  ", cn1ADOconnection)
        cb1CommandBuilder = New OleDb.OleDbCommandBuilder(daData1Adapter)
        daData1Adapter.Fill(dtCertificates)
        dtCertificates.Clear()
        'Add the certificates to inventory
        row = 0
        TempCerts = CType(Session("NewInventory"), DataTable)
        While row < TempCerts.Rows.Count
            With drNewRow2
                drNewRow2("VendorName") = TempCerts.Rows(row)(0)
                drNewRow2("certificatenumber") = TempCerts.Rows(row)(3)
                drNewRow2("CertificateValue") = 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(drNewRow2)
            daData1Adapter.Update(dtCertificates)
            row = row + 1
        End While
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of whiwex
whiwex

ASKER

Thanks
That was it.