HDokes Posted July 12, 2003 Posted July 12, 2003 I have created a form which contains a dataset with two tables. I am attempting to 'add' rows to one table however I receive an erros when doing so. Here is the code for the particular subroutine.... Private Sub btnCreateButtonRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateButtonRecords.Click ' Dim x As Integer Dim NewButtonRecord As DataRow = objdstButtonManagement.Button_Assignment_Table.NewRow ' assign 99 button records to the template For x = 1 To 99 NewButtonRecord(0) = x objdstButtonManagement.Button_Assignment_Table.Rows.Add(NewButtonRecord) Next End Sub The error I receive is: An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll Additional information: This row already belongs to this table. It should be noted that the table already has records (rows) in the table and duplicates are allowed on the key field (column) Any suggestions? Quote Thanks for any assistance provided and take care, Hdokes
JABE Posted July 14, 2003 Posted July 14, 2003 As the error msg is saying, you no longer need to call the Add method objdstButtonManagement.Button_Assignment_Table.Rows.Add(NewButtonRecord) because the row came from the NewRow method of the same datatable. Dim NewButtonRecord As DataRow = objdstButtonManagement.Button_Assignment_Table.NewRow Quote
*Experts* Nerseus Posted July 14, 2003 *Experts* Posted July 14, 2003 You need to call NewRow for every row you want to add. Try this instead: Private Sub btnCreateButtonRecords_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateButtonRecords.Click ' Dim x As Integer Dim NewButtonRecord As DataRow ' assign 99 button records to the template For x = 1 To 99 NewButtonRecord = objdstButtonManagement.Button_Assignment_Table.NewRow NewButtonRecord(0) = x objdstButtonManagement.Button_Assignment_Table.Rows.Add(NewButtonRecord) Next End Sub Add just adds a reference to an existing row (usually created with NewRow). When you try and call Add a second time on the same DataRow, you'll get the error you received - it's saying you can't have two rows in the datatable that are essentially the same objects in memory. It has nothing to do with the Key (column 0, assigned to the index "x"), which would be a different error (if you defined column 0 to be unique, for instance). -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
rufus Posted July 14, 2003 Posted July 14, 2003 You add rows to dataset by the using BindingManagerBase class to form and calling the BindingContext. When to display all the rows from the dataset, bind the textboxes that display the rows to that particular dataset and table and columnname. Now create an Instance of BindingManagerBase class and use the BindingContext in the form load page. private form_load(............) adapter.fill(dataset,"tablename") dim bc as BindingManagerBase bc=me.BindingContext(dataset,"tablename") end private sub addnewrow(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addnewrow.click me.BindingContext(dataset,"tablename").EndCurrentEdit() me.BindingContext(dataset,"tablename").AddNew() end sub Hope this would help Quote Rufus
HDokes Posted July 15, 2003 Author Posted July 15, 2003 Hi fellas.... Ok.... here's what I have.... however... while I do not get an error now.... no records show up.... am sure the routine is being run as I can set a trap and step through all 99 attemps to create a new record in the table on the form. Dim x As Integer Dim anyRow As DataRow = objdstButtonManagement.Button_Assignment_Table.NewRow Dim NewButtonRecord As DataRow For x = 1 To 99 NewButtonRecord = objdstButtonManagement.Button_Assignment_Table.NewRow NewButtonRecord(0) = x objdstButtonManagement.Button_Assignment_Table.Rows.Add(NewButtonRecord) objdstButtonManagement.Button_Assignment_Table.AcceptChanges() Next End Sub Beats the heck outta me. Correct me if I am wrong Rufus but the code you showed is what commits the table on the form to the physical data table source does it not? Quote Thanks for any assistance provided and take care, Hdokes
*Experts* Nerseus Posted July 15, 2003 *Experts* Posted July 15, 2003 What are you using to see that the records show up? The code looks good in that it should be putting 99 rows in the DataTable. You don't need the variable anyRow, or that whole line. It's not doing anything since you never add the row to the table (creating a NewRow doesn't add it, just creates an instance of a DataRow object). Also, you only need to call AcceptChanges once, after the loop. And only if that's what you really want. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
HDokes Posted July 16, 2003 Author Posted July 16, 2003 The records do not display within the dataset table in the form. Further, they do not show up after exercising a save do the underlying Access data table.Has me a bit perplexed. I have removed the anyRow reference you mentioned above... and have also mode the AcceptChanges outside the loop. It was my hope that the AcceptChange would 'dispay' the rows as they were being created. Quote Thanks for any assistance provided and take care, Hdokes
*Experts* Nerseus Posted July 16, 2003 *Experts* Posted July 16, 2003 AcceptChanges only sets an internal flag that clears any status on the row. So when you add a new row it's marked "Added". Calling AcceptChanges resets it to "Original". If you then try to save the data, the DataAdapter won't do anything since it will see all rows as "original". If you look at the DataSet after the loop, you should definitely see all 99 rows. If you *don't* call AcceptChanges for each row, they should all be marked "Added". Using a DataAdapter to insert the rows should work, assuming the InsertCommand is setup correctly. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
HDokes Posted July 17, 2003 Author Posted July 17, 2003 Hi Nerseus, I removed the 'AcceptChanges' line from the routine however it has made no difference. There is a DataAdapter associated with this dataset with a proper 'insert' statement. It should be understood that this table is a 'child' table to a form linked to a parent table. The parent table updates, inserts, and deletes just fine. Still trying to figure this one out. Quote Thanks for any assistance provided and take care, Hdokes
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.