Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Row Already belongs to this table.

Posted on 2007-09-10
2
Medium Priority
?
1,585 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:whiwex
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 19863244
just move the = dtCertificates.NewRow from the variable declaration to the loop body:

Dim row As Integer
        Dim dtCertificates As New DataTable
        Dim TempCerts As DataTable
        Dim drNewRow2 As DataRow
        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
            drNewRow2 = dtCertificates.NewRow
            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
0
 

Author Comment

by:whiwex
ID: 19863621
Thanks
That was it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question