[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3775
  • Last Modified:

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()
0
bigmoxy
Asked:
bigmoxy
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
bigmoxyAuthor Commented:
OK! I understand! I changed my code accordingly and successfully tested it!

Thank you very much!!
Tim
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now