Adding a row to dataset adds two rows to datagrid

D

dbuchanan

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?

Circumstances:
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
bmb.SuspendBinding()
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()
bmb.ResumeBinding()
bNewRowPending = False
ElseIf bEditPending Then

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

Private Sub RowToSave()

Me.localDsTables1.tblJobCustomer.AddtblJobCustomerRow( _
Me.lblpkJobCustomerId.ToString, _
Me.txtJobNumber.Text.ToString, _
Me.cboCustomerName.Text.ToStri­ng, _
Me.txtJobDescription.Text.ToString, _
Me.txtJobRefNum.Text.ToString, _
Me.txtUserNote.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
Me.BindingContext(Me.localDsTables1,
"tblJobCustomer").EndCurrentEd­it()

'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
Try
' Access the update method in the DataAccessLayer
Dim JobCustomerDT As New DAL.Tables
JobCustomerDT. UpdateDataSource (dsDataChanges)
localDsTables1.Merge(dsDataCha­nges)
localDsTables1.AcceptChanges()

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
Try
'Check to see if there are any pending changes
If (Not (ChangedRows) Is Nothing) Then
Me.daJobCustomer.Update(Change­dRows)
End If
Catch UpdateException As Exception
Throw UpdateException
End Try
Return Me.DataSet11
End Function

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

Where is the logic error?

Thank you,
Doug
 
C

Cor Ligthert

Dough,

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
dataset.

Cor
 
D

dbuchanan

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.AddtblJobCustomerRow( _
Me.lblpkJobCustomerId.ToString­, _ ' <<< Error Here.
Me.txtJobNumber.Text.ToString, _
Me.cboCustomerName.Text.ToStri­­ng, _
Me.txtJobDescription.Text.ToSt­ring, _
Me.txtJobRefNum.Text.ToString, _
Me.txtUserNote.Text.ToString)
End Sub

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top