?
Solved

Row Already belongs to this table.

Posted on 2007-09-10
2
Medium Priority
?
1,601 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

569 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