0

I'm working on a form with multiple combos boxed and one of them isn't working for me. The information is being pulled from one table in my database.

All other ones are working to find way more complex queries and it even depends on the results on the previous one, but this one is a stand-alone.

the idea is to look up the names of users in a database and display them and when selecting we use the user code (SQL):

select CashierCode, Name from cashier

results = 8011 users in the database

maybe the issue is with the number of users?

code goes as following:


 Private Sub cmbUserScanned_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbUserScanned.SelectedIndexChanged
        Try
            Application.DoEvents()
            'cmbUserScanned.Items.Clear()
            Dim strSQL As String
            Dim theList As DataTable

            strSQL = "select CashierCode, Name from cashier"
            SQL.WriteOutSQLToLogFile(strSQL)

            theList = _SQL.GetDBDataTableSimple(strSQL)
            If theList Is Nothing Then
                WriteOutToLogFile("Found no operator in the cashier table")
                Exit Sub
            End If

            cmbUserScanned.DataSource = theList
            cmbUserScanned.DisplayMember = "Name"
            cmbUserScanned.ValueMember = "CashierCode"

        Catch ex As Exception
            WriteOutToLogFile("Found no operator in the cashier table : " & ex.ToString)
        End Try
    End Sub


Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Jean Camargo
  • 340
  • 3
  • 17
  • Why would selecting an item from this ComboBox change the contents of it? This code looks like it should be in the form's constructor. Get rid of that `Application.DoEvents()`. – LarsTech Feb 08 '21 at 14:10
  • de idea debind these combo boxes are that we are appending to a SQL string options the cliend have to populate a grid. in this case we want a list of cashiers, so we can add to the SQL "and cahsiercode = ..... " and this would bring back the results for this cashier – Jean Camargo Feb 08 '21 at 14:16
  • Are there errors? What happens when you debug this code? We are missing details. – LarsTech Feb 08 '21 at 14:25

1 Answers1

2

You have to distinguish two things:

  1. Populate the drop-down part of the combobox.

  2. Populate the grid, depending on a selection in the combobox.

You seem to have mixed up these two requirements. Fix it by populating the combobox at the Form Load event:

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

    Try
        Dim strSQL As String
        Dim theList As DataTable

        strSQL = "SELECT CashierCode, Name FROM cashier"
        theList = _SQL.GetDBDataTableSimple(strSQL)
        If theList Is Nothing Then
            WriteOutToLogFile("Found no operator in the cashier table")
            Exit Sub
        End If

        cmbUserScanned.DataSource = theList
        cmbUserScanned.DisplayMember = "Name"
        cmbUserScanned.ValueMember = "CashierCode"
    Catch ex As Exception
        WriteOutToLogFile("Found no operator in the cashier table : " & ex.ToString)
    End Try
End Sub

Then when the user selects an item in the combobox, update the grid

Private Sub cmbUserScanned_SelectedIndexChanged(sender As Object, e As EventArgs) _
    Handles cmbUserScanned.SelectedIndexChanged

    If cmbUserScanned.SelectedValue Is Nothing Then ' No selection
        myGrid.DataSource = Nothing
        Exit Sub
    End If
    Try
        Dim strSQL As String
        Dim theList As DataTable

        strSQL = "SELECT Data1, Data2, ... FROM cashier_related_data WHERE CashierCode = " &
            CInt(cmbUserScanned.SelectedValue)
        theList = _SQL.GetDBDataTableSimple(strSQL)
        If theList Is Nothing Then
            WriteOutToLogFile("Found no cashier related data")
            Exit Sub
        End If

        myGrid.DataSource = theList
    Catch ex As Exception
        WriteOutToLogFile("Found no cashier related data : " & ex.ToString)
    End Try
End Sub

I also urge you to use parametrized queries. They are both: safer and more efficient.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 1
    you are correct, i mixed up the me.laod and the Handles mbUserScanned.SelectedIndexChanged I have made changes and everything is working ok. I will look into the Parametrized queires. much appreciated – Jean Camargo Feb 08 '21 at 14:56