[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ComboBox value not saved when tabbing into the field

Posted on 2011-02-21
8
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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