The following function allows saving data and handles any errors gracefully.
''' <summary> ''' Save the record ''' </summary> ''' <param name="bolConfirm"></param> ''' <returns></returns> ''' <remarks></remarks> ' ERROR: Optional parameters aren't supported in C# Private Function SaveRecord(<System.Runtime.InteropServices.OptionalAttribute(), System.Runtime.InteropServices.DefaultParameterValueAttribute( False )> ByVal bolConfirm As Boolean) As Boolean ' Return true if no save is required If Me.SaveControl.Enabled = False Then Return True End If ' The variable used to return the user response Dim drsResponse As DialogResult = Nothing ' Check if the prompt should be displayed If bolConfirm Then ' Prompt the user to save drsResponse = MessageBox.Show("Do you want to save the data?", "Save?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) Else ' Default to Yes drsResponse = Windows.Forms.DialogResult.Yes End If ' Check the response Select Case drsResponse Case Windows.Forms.DialogResult.Yes Try ' Verifies the value of the control losing focus by causing the Validating and Validated events to occur, in that order. Me.Validate() ' Applies pending changes to the underlying data source. Me.ContactBindingSource.EndEdit() ' Sends changes back to the database Me.ContactTableAdapter.Update(Me.EmployeesDataSet.Contact) ' Allow adding a new item Me.BindingNavigatorAddNewItem.Enabled = True ' Allow deleting deleting the item Me.BindingNavigatorDeleteItem.Enabled = True ' Disable the save control Me.SaveControl.Enabled = False ' A successful save Return True Catch ex As Exception ' Cause an unsuccessful save Return False End Try Exit Select Case Windows.Forms.DialogResult.No ' Cancels the current edit operation Me.ContactBindingSource.CancelEdit() ' Disable the save control Me.SaveControl.Enabled = False ' Still a successful save even if they choose No Return True Case Windows.Forms.DialogResult.Cancel ' Enable the save control Me.SaveControl.Enabled = True ' A un-successful save as they choose Cancel Return False End Select End Function