filtering the second combobox based on index on the first combobox

安稳与你 提交于 2019-12-25 01:52:46

问题


   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


回答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.




回答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



回答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




回答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



回答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



来源:https://stackoverflow.com/questions/15805148/filtering-the-second-combobox-based-on-index-on-the-first-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!