Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

ComboBox value not saved when tabbing into the field

I have a Windows VB.NET form that is bound to a table in my database.  The form has text fields and combo box fields.  I am having a very interesting problem and can't figure out how to fix it.  When the form is displayed, if I use the tab key to enter the combo box field; then use the down arrow key to change the value in the combo box; then tab to the next field; then click on the Save button, the value in the combo box reverts back to the original value.  When I change it, it changes on the screen but as soon as I click on the Save button it reverts back to the orginal value.  In my Save Function I call EndEdit() on the BindingSource and that is when the value reverts back to the original value.

However, if I click the combo box with my mouse; then use the down arrow key to change the value of the combo box; then click on the Save button, the new value is kept and saved.  Calling EndEdit on the Binding Source doesn't change the value.

I can't figure out what is going on.  I'm including the two functions that I think come into play with this.

Any help is greatly appreciated.
========= This function is triggered whenever the combobox value is changed ====================
    Private Sub EmployeeStatusComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeStatusComboBox.SelectedIndexChanged
        ' This function is called even when the window is initially loading, therfore
        ' skip processing a status change since it was handled in the Load Event
        Dim NewStatus As String

        If Not InitialLoadFlag Then

            NewStatus = StrConv(EmployeeStatusComboBox.Text, VbStrConv.Uppercase)
            ' Check if Employement Status has changed
            If NewStatus <> OriginalEmployeeStatus Then
                Select Case NewStatus
                    Case "ACTIVE"
                        'set the items in the list accordingly
                        With PlanStatusComboBox.Items
                            .Clear() 'clear the current options
                            .Add("FULL TIME")
                            .Add("PART TIME")
                        End With

                        'select full time
                        EmployeeDrow.EmployeeStatus = "ACTIVE"
                        EmployeeDrow.PlanStatus = "FULL TIME"
                        If Not PlanStatusComboBox.Enabled Then
                            PlanStatusComboBox.Enabled = True
                        End If
                        PlanStatusComboBox.Text = "FULL TIME"
                        PlanStatusComboBox.Refresh()

                    Case "TERMINATED"
                        'set the items in the list accordingly
                        With PlanStatusComboBox.Items()
                            .Clear() 'clear the current options
                            .Add("NON-VESTED")
                            .Add("VESTED") 'left for some reason
                        End With
                        EmployeeDrow.EmployeeStatus = "TERMINATED"
                        EmployeeDrow.PlanStatus = "NON-VESTED"
                        If Not PlanStatusComboBox.Enabled Then
                            PlanStatusComboBox.Enabled = True
                        End If
                        PlanStatusComboBox.Text = "NON-VESTED"
                        PlanStatusComboBox.Refresh()
                    Case "NON-BARGAINING"
                        'set the items in the list accordingly
                        With PlanStatusComboBox.Items()
                            .Clear() 'clear the current options
                            .Add("NON-VESTED")
                            .Add("VESTED")
                        End With
                        EmployeeDrow.EmployeeStatus = "NON-BARGAINING"
                        EmployeeDrow.PlanStatus = "NON-VESTED"
                        If Not PlanStatusComboBox.Enabled Then
                            PlanStatusComboBox.Enabled = True
                        End If
                        PlanStatusComboBox.Text = "NON-VESTED"
                        PlanStatusComboBox.Refresh()
                End Select
            End If
            EmployeeStatusComboBox.Refresh()
        Else
            InitialLoadFlag = False
        End If
    End Sub

============  This is the Save function =========================
    Private Function SaveEmployeeData() As Boolean
        Dim success As Integer
        Dim ec As New EmployeeClass
        Dim rmBirthDate As Boolean = False
        Dim rmTermDate As Boolean = False
        Dim rmDBDate As Boolean = False
        Dim rmBFRDate As Boolean = False
        Dim rmAVDate As Boolean = False

        ' Validate Employee Data
        If Not ValidateEmployeeRecord() Then
            Return False
        End If

        ' Save information
        Try
            Me.EmployeesBindingSource.EndEdit()
            If EmployeeDrow.RowState = DataRowState.Modified Or EmployeeDrow.RowState = DataRowState.Added Then
                ' Convert Text fields to uppercase
                EmployeeDrow.Address = RemoveCrLf(UCase(EmployeeDrow.Address))
                EmployeeDrow.City = UCase(EmployeeDrow.City)
                EmployeeDrow.EmployeeStatus = UCase(EmployeeDrow.EmployeeStatus)
                EmployeeDrow.EmployeeType = UCase(EmployeeDrow.EmployeeType)
                EmployeeDrow.FirstName = UCase(EmployeeDrow.FirstName)
                EmployeeDrow.LastName = UCase(EmployeeDrow.LastName)
                EmployeeDrow.MaritalStatus = UCase(EmployeeDrow.MaritalStatus)
                EmployeeDrow.Notes = UCase(EmployeeDrow.Notes)
                EmployeeDrow.Sex = UCase(EmployeeDrow.Sex)
                EmployeeDrow.State = UCase(EmployeeDrow.State)
                ' Remove DB Amount if termination date entered for first time
                If OriginalTermDate = "" Then
                    Try
                        If IsDate(EmployeeDrow.TerminationDate) Then
                            EmployeeDrow.LifeInsuranceFaceAmount = 0
                            LifeInsuranceFaceAmountTextBox.Text = ""
                        End If
                    Catch ex As Exception

                    End Try
                End If

                ' Check if dates need to be removed
                If BirthDateMaskedTextBox.Text = "" Then
                    rmBirthDate = True
                End If
                If TerminationDateTextBox.Text = "" Then
                    rmTermDate = True
                End If
                If EffectiveDateforLifeInsuranceTextBox.Text = "" Then
                    rmDBDate = True
                End If
                If BenefitFormsReceviedTextBox.Text = "" Then
                    rmBFRDate = True
                End If
                If AccountValuationTextBox.Text = "" Then
                    rmAVDate = True
                End If

                success = EmployeesTableAdapter.Update(PfmsDataSet1.Employees)
                If success = 0 Then
                    MsgBox("Employee Information Not Saved.")
                Else
                    ' Check if dates need to be removed
                    If rmBirthDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "DOB") Then
                            MsgBox("Could not remove Employee Birth Date.")
                        End If
                    End If
                    If rmTermDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "DOT") Then
                            MsgBox("Could not remove Employee Termination Date.")
                        End If
                    End If
                    If rmDBDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "DBD") Then
                            MsgBox("Could not remove Employee Death Benefit Date.")
                        End If
                    End If
                    If rmBFRDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "BFRD") Then
                            MsgBox("Could not remove Employee Benefit Forms Received Date.")
                        End If
                    End If
                    If rmAVDate Then
                        If Not ec.RemoveDate(EmployeeDrow.EmployeeID, "AVD") Then
                            MsgBox("Could not remove Employee Account Valuation Date.")
                        End If
                    End If

                    OriginalName = EmployeeDrow.FirstName + " " + EmployeeDrow.LastName
                    OriginalSSN = EmployeeDrow.SocialSecurityNumber
                    Try
                        OriginalTermDate = EmployeeDrow.TerminationDate
                    Catch ex As Exception
                        OriginalTermDate = ""
                    End Try
                    ' Store original employment status in case it changes
                    OriginalEmployeeStatus = StrConv(EmployeeDrow.EmployeeStatus, VbStrConv.Uppercase)
                    OriginalPlanStatus = StrConv(EmployeeDrow.PlanStatus, VbStrConv.Uppercase)

                    MsgBox("Employee Information Saved.")
                    Return True
                End If
            Else
                Return True
            End If
        Catch ex As Exception
            ' Check if duplicate SSN error
            If ex.Message.Contains("duplicate") Then
                MsgBox("SSN already exists.")
            Else
                If MsgBox("Error saving employee. Click No to Continue.  Click Yes to Display Error Details.", MsgBoxStyle.YesNo, "Error") = MsgBoxResult.Yes Then
                    MsgBox("Error: " + ex.ToString())
                End If
            End If
        End Try
        Return False
    End Function

Open in new window

0
dyarosh
Asked:
dyarosh
1 Solution
 
rawinnlnx9Commented:
For the AutoPostback property of the control set it equal to true. That should do it.
0
 
PagodNaUtakCommented:
Change this line of code "If Not InitialLoadFlag Then" to one I posted below.
If Not InitialLoadFlag AndAlso Not EmployeeStatusComboBox.SelectedIndex < 0  Then

Open in new window

0
 
dyaroshAuthor Commented:
rawinnlnx9:
I could not find the AutoPostBack property.  See attached screen shot.

PagodNaUtak:
I tried your code and it did not work. The value still reverts back to the original value if I use Tab. Screen Shot of Employment Status field
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ElrondCTCommented:
Is this a web form (ASP.NET), or a Windows form? If it's a Windows form, that would explain why you don't have AutoPostBack--that's a web method.

Which version of Visual Studio/Visual Basic are you using? 2008 or 2010?
0
 
CodeCruiserCommented:
>I have a Windows VB.NET form that is bound to a table in my database.
The question begins with above sentence!

The reason this is happening is that changing the value using arrows does not turn on the edit mode of the combobox hence the new selection is not registered. You can use the .Text property of the combobox to read the new selection, find the id of the selected text and use this id when saving. Also try calling the BeginEdit in GotFocus event of the combobox.
0
 
dyaroshAuthor Commented:
My apologies for taking so long to respond.  Thank you for your help.
0
 
CodeCruiserCommented:
Glad to help :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now