SQL criteria using combobox

newdbo

Active member
Joined
Aug 23, 2009
Messages
25
Programming Experience
Beginner
Hi all, needed help to set the selection criteria using combobox, but there is a possibility to select "All Items" option. There are more than 2 combobox selection. Really need advise...

VB.NET:
strSQL = "SELECT nama from GridView"
        Dim da As New SqlDataAdapter(strSQL, oconnection)
        Dim ds As New DataSet
        da.Fill(ds, "name")
        Dim dt As New DataTable
        'dt = ds.Tables(0)

        dt.Columns.Add("name", GetType(System.String))
        Dim drDSRow As DataRow
        Dim drNewRow As DataRow

        dt.Rows.Add("*", "All names") ---->just the toppest row that needed the selected value

        For Each drDSRow In ds.Tables("name").Rows()
            drNewRow = dt.NewRow
            drNewRow("name") = drDSRow("name")
            dt.Rows.Add(drNewRow)
        Next


        ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

        With ComboBox1
            .DataSource = dt
            .DisplayMember = "nama"
            '.SelectedIndex = 0

        End With
 
Last edited:
Sorry, i forgot to write the code using "Where" clause, but at last i have resolved it. this is the code, hope this will help people who has the same problem like i me
VB.NET:
        strSQL1 = " SELECT DISTINCT(ACC) FROM " & Trim(deptGStbl) & " WHERE PT ='" & Trim(cbPT.SelectedValue.ToString) & "'"
        If cbPT.SelectedIndex = 0 Then
            strSQL1 = strSQL1.Replace("=", " like ")
        End If
        strSQL2 = " AND KDST ='" & Trim(cbKDST.SelectedValue.ToString) & "'"
        If cbKDST.SelectedIndex = 0 Then
            strSQL2 = strSQL2.Replace("=", " like ")
        End If
        strSQL3 = " AND KDKB='" & Trim(cbKDKB.SelectedValue.ToString) & "'"
        If cbKDKB.SelectedIndex = 0 Then
            strSQL3 = strSQL3.Replace("=", " like ")
        End If
        strSQL = strSQL1 & strSQL2 & strSQL3




        Dim dacbAccGS As New SqlDataAdapter()
        dacbAccGS.SelectCommand = New SqlCommand(strSQL, oconnection)
        ' Create DataSet, fill it and view in data grid
        Dim dscbAccGS As New DataSet
        dacbAccGS.Fill(dscbAccGS, "acc")
        Dim dtcbAccGS As New DataTable

        dtcbAccGS.Columns.Add("account", GetType(System.String))
        dtcbAccGS.Columns.Add("value", GetType(System.String))
        Dim drDSRow As DataRow
        Dim drNewRow As DataRow

        drNewRow = dtcbAccGS.NewRow
        drNewRow("account") = "All Account"
        drNewRow("value") = "%"
        dtcbAccGS.Rows.Add(drNewRow)

        For Each drDSRow In dscbAccGS.Tables("acc").Rows()
            drNewRow = dtcbAccGS.NewRow
            drNewRow("account") = drDSRow("acc")
            drNewRow("value") = drDSRow("acc")
            dtcbAccGS.Rows.Add(drNewRow)
        Next


        cbAccount.DropDownStyle = ComboBoxStyle.DropDownList

        With cbAccount
            .DataSource = dtcbAccGS
            .DisplayMember = "account"
            .ValueMember = "value"
            '.SelectedIndex = 0

        End With
Thank you
 

Latest posts

Back
Top