471,059 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,059 software developers and data experts.

Adding a row to dataset adds two rows to datagrid

Where is the logic error that causes the attached code to adds two rows
instead on one to the datagrid?

The code correctly update the datasource with one row, but the datagrid
gets two new rows.

What can I do to correct this?

There are textboxes on the form where the users will enter data. The
textboxes and the datagrid share the databinding. After the user enters
data into the code behind the save button assembles a datarow and adds
it to the dataset. Then the dataset is updated to the datasource.

You may note that the dataset is containded in a referenced project - a
data access layer (DAL.Tables)

================================================== ======
Private bmb As BindingManagerBase
Private bNewRowPending as Boolean
Private bEditPending as Boolean
Private Sub CreateBindings()
' Code for binding not shown
bmb = Me.BindingContext(localDsTables1.tblJobCustomer)
End Sub

Private Sub AddNewRow() ' Called by btnAddNewRow button
Call ClearDataEntryControlsForNewRow()
bNewRowPending = True
'User enters data into the controls
End Sub

Private Sub SaveRowChanges() ' Save button calls this

' Determine if it is an Added or Edited Row
If bNewRowPending Then

' Code that adds a new row
Call RowToSave() ' See code below
bmb.Position = bmb.Count
Call UpdateDataSet()
bNewRowPending = False
ElseIf bEditPending Then

' Code that saves edited rows
bEditPending = False
End If
End Sub

Private Sub RowToSave()

Me.localDsTables1.tblJobCustomer.AddtblJobCustomer Row( _
Me.lblpkJobCustomerId.ToString, _
Me.txtJobNumber.Text.ToString, _
Me.cboCustomerName.Text.ToStri*ng, _
Me.txtJobDescription.Text.ToString, _
Me.txtJobRefNum.Text.ToString, _
End Sub

Private Sub UpdateDataSet() 'in the DataAccessLayer

'New DataSet to hold changes
Dim dsDataChanges As New DAL.dsTables 'the DataSet

'Stop any current edits

'Get the changes that have been made to the dataset
dsDataChanges = CType(Me.localDsTables1.GetChanges,
DAL.dsTables) 'the DataSet

'Check to see if any changes are pending
If (Not (dsDataChanges) Is Nothing) Then
' Access the update method in the DataAccessLayer
Dim JobCustomerDT As New DAL.Tables
JobCustomerDT. UpdateDataSource (dsDataChanges)

Catch UpdateDataSetException As Exception
Throw UpdateDataSetException
End Try '

End If

=== Code in the DataAccessLayer ===

Public Function UpdateDataSource(ByVal ChangedRows As DAL.dsTables)
As dsTables '<< the xsd
'Check to see if there are any pending changes
If (Not (ChangedRows) Is Nothing) Then
End If
Catch UpdateException As Exception
Throw UpdateException
End Try
Return Me.DataSet11
End Function

================================================== ======

Where is the logic error?

Thank you,

Nov 21 '05 #1
2 1532

This is mostly hard to see.

However the most change is that you have (expressly when you use
autoincrement) filled your dataset again and the new row is added to the old

Nov 21 '05 #2
Thank you for your replay.

I found the error.
Me.lblpkJobCustomerId.ToString*, _ ' <<< Error Here.
Me.lblpkJobCustomerId.Text.ToString*, _ ' <<< Correction

This entered "SystemWIndows.Form.Label, Text: 4c" into my GUID string.
(I was using a form generated GUID for my ID key)
My most foolish mistake was in having the ID field hidden while trying
to troublshoot the problem. With the ID field shown I could then see
the unexpected entry.

Private Sub RowToSave()
Me.localDsTables1.tblJobCustom*er.AddtblJobCustome rRow( _
Me.lblpkJobCustomerId.ToString*, _ ' <<< Error Here.
Me.txtJobNumber.Text.ToString, _
Me.cboCustomerName.Text.ToStri**ng, _
Me.txtJobDescription.Text.ToSt*ring, _
Me.txtJobRefNum.Text.ToString, _
End Sub

Thank you.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Bruce Stockwell | last post: by
2 posts views Thread by Clayton Hamilton | last post: by
3 posts views Thread by Jim Heavey | last post: by
1 post views Thread by Sebastian Santacroe | last post: by
3 posts views Thread by rcoco | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.