Solved

ComboBox value not saved when tabbing into the field

Posted on 2011-02-21
8
375 Views
Last Modified: 2012-05-11
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
Comment
Question by:dyarosh
8 Comments
 
LVL 9

Expert Comment

by:rawinnlnx9
ID: 34947056
For the AutoPostback property of the control set it equal to true. That should do it.
0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 34948806
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
 

Author Comment

by:dyarosh
ID: 34961024
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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 20

Expert Comment

by:ElrondCT
ID: 35009010
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 35021178
>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
 

Author Closing Comment

by:dyarosh
ID: 35165013
My apologies for taking so long to respond.  Thank you for your help.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35179275
Glad to help :-)
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get Client IP on RDS - VB.NET 15 47
Help with error when uploading excel file 3 29
HTML - Color not displaying correctly in EMAIL. 6 38
Store results in vb.net 3 22
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question