ivandinglasan
ivandinglasan

Reputation: 384

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 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)
    cmbCategory.DataSource = dt
    cmbCategory.DisplayMember = "SUBCAT_Name"
    cmbCategory.ValueMember = "SUBCAT_ID"
    sqlconn.Close()
End Sub

when i put this code on the form load event the data on the first combo box dissappears also when i put this code on the index_changed of the first combobox

but when i commented this code it displays the records again in combobox 1

i need to filter the SUB_CATEGORY_COMBO_BOX based on the CATEGORY_COMBOBOX

Upvotes: 0

Views: 1740

Answers (5)

ivandinglasan
ivandinglasan

Reputation: 384

 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

Upvotes: 0

Kasnady
Kasnady

Reputation: 2279

 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

Upvotes: 0

Thangamani  Palanisamy
Thangamani Palanisamy

Reputation: 5290

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

Upvotes: 0

Ken Clark
Ken Clark

Reputation: 2530

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.

Upvotes: 1

WernerW
WernerW

Reputation: 812

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

Upvotes: 0

Related Questions