Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Thanks for any assistance provided and take care,

 

Hdokes

Posted

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

  • *Experts*
Posted

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

"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
Posted

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

Rufus
Posted

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?

Thanks for any assistance provided and take care,

 

Hdokes

  • *Experts*
Posted

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

"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
Posted

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.

Thanks for any assistance provided and take care,

 

Hdokes

  • *Experts*
Posted

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

"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
Posted

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.

Thanks for any assistance provided and take care,

 

Hdokes

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...