none
Data form wizard inserts a duplicate row (visually on the datagrid) when the first row is entered RRS feed

  • Question

  • Hi everyone,

    Apologies if this thread has been covered elsewhere.  I have searched on the topic but to no avail.

     

    I'm still fairly new to .NET development so decided to use a data form wizard to generate a SQL connection, data adapter, dataset and datgrid. 

     

    Inserting and updating records generally works fine Smile

     

    However... when the form is loaded for the first time, and a record entered for the first time, when I save the record back to the database the datagrid shows it twice in the grid.  (The duplicated row underneath the first entered row). Closing the form and re-opening, correctly shows just a single record.  But why was it duplicated in the grid?

     

    Please see code below if it throws any ideas?

     

    Regards,

    Ynyr

     

    Code Snippet

     

    Public Sub UpdateDataSet()

    'Create a new dataset to hold the changes that have been made to the main dataset.

    Dim objDataSetChanges As FrontEnd.dsDemandProcessingItem = New FrontEnd.dsDemandProcessingItem

    'Stop any current edits.

    Me.BindingContext(objdsDemandProcessingItem, "tblDemandProcessingItems").EndCurrentEdit()

    'Get the changes that have been made to the main dataset.

    objDataSetChanges = CType(objdsDemandProcessingItem.GetChanges, FrontEnd.dsDemandProcessingItem)

    'Check to see if any changes have been made.

    If (Not (objDataSetChanges) Is Nothing) Then

    Try

    'There are changes that need to be made, so attempt to update the datasource by

    'calling the update method and passing the dataset and any parameters.

    Me.UpdateDataSource(objDataSetChanges)

    objdsDemandProcessingItem.Merge(objDataSetChanges)

    objdsDemandProcessingItem.AcceptChanges()

    Catch eUpdate As System.Exception

    'Add your error handling code here.

    Throw eUpdate

    End Try

    'Add your code to check the returned dataset for any errors that may have been

    'pushed into the row object's error.

    End If

    End Sub

     

    Private Sub frmDemandProcessingItem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Try

    'Attempt to load the dataset.

    lblLastSaved.Text = ""

    objdsDemandProcessingItem.Tables("tblDemandProcessingItems").Columns(1).DefaultValue = g_DemandRef.ToUpper

    OleDbDataAdapter1.SelectCommand.CommandText = OleDbDataAdapter1.SelectCommand.CommandText & " WHERE DemandRef = '" & g_DemandRef & "' "

    Me.LoadDataSet()

    Catch eLoad As System.Exception

    'Add your error handling code here.

    'Display error message, if any.

    System.Windows.Forms.MessageBox.Show(eLoad.Message)

    End Try

    End Sub

     

    Tuesday, July 24, 2007 9:13 AM