Skip to content Skip to sidebar Skip to footer

Filtering The Second Combobox Based On Index On The First Combobox

Public Sub FiltercmbSubCategory() Dim sqlconn As New SqlClient.SqlConnection sqlconn.ConnectionString = 'server = SKPI-APPS1;' & _ 'Database = EOEMS;integrated s

Solution 1:

Bind your first combobox inside:

if (!Page.IsPostBack)
{
  //  Bind combobox1 code here;
}

Now, on selectedindexchange call the code for binding subcategory combo.

Also, have look on your code again:

Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentSubCategory WHERE SUBCAT_ID = '" & cmbCategory.Text & "'", sqlconn)
Dim dt As New DataTable
da.Fill(dt)
cmbCategory.DataSource = dt
cmbCategory.DisplayMember = "SUBCAT_Name"
cmbCategory.ValueMember = "SUBCAT_ID"

In this code you are passing cmbCategory.Text as paramater for binding same dropdown cmbCategory. I think you have missed your second dropdown here. May be I am not correct but, it seems like that.


Solution 2:

Is subcat_id a string or integer? If its integer drop the single quotes in your where clause.

Also to me it seems you are filling the same combobox that you select from with data, and not the sub box ?

    cmbCategory[SUB?].DataSource = dt

Solution 3:

You have used same object cmbCategory for both SUB_CATEGORY_COMBO_BOX based on the CATEGORY_COMBOBOX

You should use cmbSubCategory for SUB_CATEGORY_COMBO_BOX and cmbCategory for CATEGORY_COMBOBOX

 Public Sub FiltercmbSubCategory()
            Dim sqlconn As New SqlClient.SqlConnection
            sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
            "Database = EOEMS;integrated security=true"

            Dim sqlcommand As SqlCommand

            sqlconn.Open()
            Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentSubCategory WHERE SUBCAT_ID = '" & cmbCategory.Text & "'", sqlconn)
            Dim dt As New DataTable
            da.Fill(dt)
            cmbSubCategory.DataSource = dt
            cmbSubCategory.DisplayMember = "SUBCAT_Name"
            cmbSubCategory.ValueMember = "SUBCAT_ID"
            cmbSubCategory.databind();
            sqlconn.Close()
        End Sub

Hope this helps


Solution 4:

 Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Items.Add(ComboBox1.SelectedIndex)
    End Sub

I think is like this, what you want is, what you choose on 1combobox, it will display to combobox2 ?is like that?

UPDATE

     Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 

sqlconn.Open()
    Dim da1 As New SqlDataAdapter("SELECT Column_name FROM tblOfficeEquipmentSubCategory 
WHERE column_name_ = '" % & combobox1.selecteditem & % "'", sqlconn)
   From here >>> Dim dt1 As New DataTable
    da1.Fill(dt)
    cmbsubCategory.DataSource = dt1
    cmbsubCategory.DisplayMember = "SUBCAT_Name"
    cmbsubCategory.ValueMember = "SUBCAT_ID"
    sqlconn.Close()
                    End Sub <<< till here, you need to change yourself

Solution 5:

 Dim sqlconn As New SqlClient.SqlConnection
    sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
    "Database = EOEMS;integrated security=true"

    Dim dt As New DataTable

    sqlconn.Open()
    Dim da As New SqlDataAdapter("SELECT SUBCAT_ID FROM tblOfficeEquipmentSubCategory WHERE CAT_ID = '" & cmbCategory.Text & "'", sqlconn)
    da.Fill(dt)
    cmbSubCategory.DataSource = dt
    cmbSubCategory.DisplayMember = "SUBCAT_Name"
    cmbSubCategory.ValueMember = "SUBCAT_ID"
    sqlconn.Close()

got the correct answer thanks everyone


Post a Comment for "Filtering The Second Combobox Based On Index On The First Combobox"