简体   繁体   中英

Combo box not displaying information VB.NET

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


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM