By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,243 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

Adding a row to dataset adds two rows to datagrid

P: n/a
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.AddtblJobCustomer Row( _
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

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
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, _
Me.txtUserNote.Text.ToString)
End Sub

Thank you.

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.