Please help me with "This row already belongs to this table" error

I'm creating a dataset table to be used in a repeater and want to add rows as I format the data. My problem is that I get this error: System.ArgumentException: This row already belongs to this table. I googled the error however the results all seem to discuss the row belonging to another table or the adding of rows to an existing table. I am adding rows to a new table.

I ran my code the first time without "optionsTable.NewRow()" for second and subsequent rows. However after getting the same error I thought I needed it to allocate the row before adding data to it. It didn't make a difference though.

Please help!
Regards,
Tim


        Dim optionsTable As New DataTable("Options")
        optionsTable.Columns.Add("Detail", GetType(System.String))
        DS.Tables.Add(optionsTable)
        Dim detailRow As DataRow = optionsTable.NewRow()
        detailRow("Detail") = "<tr>" & _
        "<td><input name='option1' type='checkbox' disabled='disabled' " & ChromeBumperOption & "/>Chrome Bumper</td>" & _
        "<td><input name='option2' type='checkbox' disabled='disabled' " & AntiLockBrakesOption & "/>Anti Lock Brakes</td>" & _
        "<td><input name='option3' type='checkbox' disabled='disabled' " & AMFMRadioOption & "/>AM/FM Radio</td>" & _
        "</tr>"
        optionsTable.Rows.Add(detailRow)
***    optionsTable.NewRow()
        detailRow("Detail") = "<tr>" & _
        "<td><input name='option4' type='checkbox' disabled='disabled' " & PaintedBumperOption & "/>Painted Bumper</td>" & _
        "<td><input name='option5' type='checkbox' disabled='disabled' " & FrontDiscBrakesOption & "/>Front Disc Brakes</td>" & _
        "<td><input name='option6' type='checkbox' disabled='disabled' " & CassetteOption & "/>Cassette</td>" & _
        "</tr>"
        optionsTable.Rows.Add(detailRow)

... more code to add rows ...

        options.DataSource = DS.Tables("Options").DefaultView
        options.DataBind()
bigmoxyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pratima PharandeCommented:
try like this --ad optionsTable to Ds at end

        Dim optionsTable As New DataTable("Options")
        optionsTable.Columns.Add("Detail", GetType(System.String))
       
        Dim detailRow As DataRow = optionsTable.NewRow()
        detailRow("Detail") = "<tr>" & _
        "<td><input name='option1' type='checkbox' disabled='disabled' " & ChromeBumperOption & "/>Chrome Bumper</td>" & _
        "<td><input name='option2' type='checkbox' disabled='disabled' " & AntiLockBrakesOption & "/>Anti Lock Brakes</td>" & _
        "<td><input name='option3' type='checkbox' disabled='disabled' " & AMFMRadioOption & "/>AM/FM Radio</td>" & _
        "</tr>"
        optionsTable.Rows.Add(detailRow)
***    optionsTable.NewRow()
        detailRow("Detail") = "<tr>" & _
        "<td><input name='option4' type='checkbox' disabled='disabled' " & PaintedBumperOption & "/>Painted Bumper</td>" & _
        "<td><input name='option5' type='checkbox' disabled='disabled' " & FrontDiscBrakesOption & "/>Front Disc Brakes</td>" & _
        "<td><input name='option6' type='checkbox' disabled='disabled' " & CassetteOption & "/>Cassette</td>" & _
        "</tr>"
        optionsTable.Rows.Add(detailRow)
DS.Tables.Add(optionsTable)
0
bigmoxyAuthor Commented:
I replaced optionsTable.NewRow() with DS.Tables.Add(optionsTable) and now get System.ArgumentException: DataTable already belongs to this DataSet.
0
Bob LearnedCommented:
It sounds like you have a DataSet that is external to that code, that is keeping a reference to the DataTable.

Bob
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

bigmoxyAuthor Commented:
This code is part of my Page_Load where the dataset is is declared. How do I resolve the problem?

    Private Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load

        Dim dbPath As String = Server.MapPath("currentDB/CVGWebsite.mdb")
        Dim ID As String = Request.QueryString("id")
        Dim MyConnection As New OleDbConnection _
            ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath & _
             ";Persist Security Info=False;")
        Dim detailCommand As New OleDbDataAdapter("SELECT DISTINCT Color, Engine," & _
            " fld15Status, GVW, Make, Mileage, Model, nbrVIN, Price, [Stock Number]," & _
            " Transmission, [Vehicle Category], [Vehicle Type], [Wheel Size], Year" & _
            " FROM WebData" & _
            " WHERE [Stock Number] = '" & ID & "'", MyConnection)
        Dim optionCommand As New OleDbDataAdapter("SELECT Option" & _
            " FROM WebData" & _
            " WHERE [Stock Number] = '" & ID & "'", MyConnection)

        Dim DS As DataSet = New DataSet()
... more code ...
        code from above problem
... more code ...
        options.DataSource = DS.Tables("Options").DefaultView
        options.DataBind()

    End Sub
0
Bob LearnedCommented:
Does this exception occur the first time the page is loaded, or when the page is posted back?  I can't see any other reason for this problem (your code looks good).

Bob
0
bigmoxyAuthor Commented:
It occurs on the initial page load. I opened a new browser, cleared cache and then went to the page to be sure.
0
Bane83Commented:
I believe your problem is the re-use of the detailRow variable.  This is a reference type variable and as such it points to a position in memory.  Calling detailRow = optionsTable.NewRow() doesn't actually change the point in memory that the variable is pointing to, it just resets its contents to a new row.  Therefore, detailRow is already a part of optionsTable and when you try to re-add it, the error is thrown.

I'm not terribly familiar with VB.NET syntax, but you might want to try:

ReDim detailRow As DataRow = optionsTable.NewRow()

after each AddRow(detailRow).  Failing that, you'll probably have to create a new DataRow variable for each row of data.
0
bigmoxyAuthor Commented:
Here is my entire code behind file for review.
vehicleDetails.aspx.vb.txt
0
Bob LearnedCommented:
For each row that you are trying to create for the options table, you need to create a new instance:

detailRow = optionsTable.NewRow()

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bigmoxyAuthor Commented:
OK! I understand! I changed my code accordingly and successfully tested it!

Thank you very much!!
Tim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.