Tamseyc
Tamseyc

Reputation: 445

Dynamic drop downs in Visual Basic 2008

I have an application which is using a set of three combo boxes. I have managed to populate each drop down depending on the selected value of the previous one The only problem is that, since the data is coming the database two selected values from the first combo box may have two different number of items in the database. so the second combobox should be populated with such data each time the selectedIndex_changed nethod of the first combobox is invoked

if for example, the first combo box's first item has 10 corresponding items in the second combo box and a first combo box's second item has three corresponding items in the second combo box, selecting the second item after selecting the first item will lead to second combobox showing the three items and seven empty entries trailing after the three.

What I want is to have the second combobox loaded with three items exactly when the second item in the first combo box has three items in the database

here is an example

Private Sub cboAccountGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As   System.EventArgs) Handles cboAccountGroup.SelectedIndexChanged
    If mblnAccountGroupFirstLoad = True Then
        Exit Sub
    End If
    Dim index As String
    index = cboAccountGroup.SelectedValue
    'Call clearDataEntries()
    Dim psql As String
    psql = "Select Account_Type_ID,Description from Tbl_Account_Type Where Account_Group_id=" & index
    Call setDropDowns(cboAccountType, psql)
    mblnAccountTypeFirstLoad = False

End Sub

The setDropDowns is defined as follows

 Public Sub setDropDowns(ByRef combo As ComboBox, ByVal sql As String)


    Try
        Dim adaptor As New SqlDataAdapter(sql, mcon)
        Dim dataset As New DataTable()
        mcon.Open()
        adaptor.Fill(DataSet)
        mcon.Close()

        combo.DataSource = DataSet
        combo.DisplayMember = DataSet.Columns(1).ColumnName
        combo.ValueMember = DataSet.Columns(0).ColumnName


    Catch ex As Exception
    Finally
        'mcon.Close()

    End Try

End Sub

Please assist.

Upvotes: 0

Views: 825

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 416179

I'd change the code up somewhat to look more like this:

'Build functions that return data, and keep them separate from code that updates your UI
Public Function GetAccountTypesByAccountID(ByVal AccountGroupID As Integer) As DataTable

    Dim sql As String = "SELECT Account_Type_ID,Description FROM Tbl_Account_Type Where Account_Group_id= @AccountGroupID"
    Dim result As New DataTable()

    'Create a new connection each time. Really! Thanks to connection pooling, this is the way to go
    Using mcon As New SqlConnection(GetConnectionString()), _
          cmd As New SqlCommand(sql, mcon)

        'USE PARAMETERIZED QUERIES!!
        cmd.Parameters.Add("@AccountGroupID", SqlDbTypes.Int).Value = AccountGroupID

        'The Using block will make sure the connection is closed, even if an exception is thrown
        mcon.Open()
        Using rdr As SqlDataReader = cmd.ExecuteReader()
            result.Load(rdr)
        End Using
    End Using
    Return result
End Function


Private Sub cboAccountGroup_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As   System.EventArgs) Handles cboAccountGroup.SelectedIndexChanged
    If mblnAccountGroupFirstLoad Then Exit Sub
    mblnAccountTypeFirstLoad = False

    ' clearDataEntries()

    Dim AccountTypes As DataTable = GetAccountTypesByAccountID(Integer.Parse(cboAccountGroup.SelectedValue))

    cboAccountType.DisplayMember = AccountTypes.Columns(1).ColumnName
    cboAccountType.ValueMember   = AccountTypes.Columns(0).ColumnName
    cboAccountType.DataSource = AccountTypes 'Set datasoure LAST

End Sub

Upvotes: 1

Related Questions