How do I add rows programatically to a dataset table?

HDokes

Active member
Joined
Jun 28, 2003
Messages
32
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?
 
As the error msg is saying, you no longer need to call the Add method
[VB]
objdstButtonManagement.Button_Assignment_Table.Rows.Add(NewButtonRecord)
[/VB]

because the row came from the NewRow method of the same datatable.

[VB]
Dim NewButtonRecord As DataRow = objdstButtonManagement.Button_Assignment_Table.NewRow
[/VB]
 
You need to call NewRow for every row you want to add. Try this instead:
Code:
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, youll get the error you received - its saying you cant 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
 
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
 
Hi fellas....

Ok.... heres 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?
 
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 dont need the variable anyRow, or that whole line. Its not doing anything since you never add the row to the table (creating a NewRow doesnt add it, just creates an instance of a DataRow object).

Also, you only need to call AcceptChanges once, after the loop. And only if thats what you really want.

-Nerseus
 
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.
 
AcceptChanges only sets an internal flag that clears any status on the row. So when you add a new row its marked "Added". Calling AcceptChanges resets it to "Original". If you then try to save the data, the DataAdapter wont 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 *dont* 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
 
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.
 
Back
Top