0

I have a query which selects from the db where value=combobox.selectedtext.

However, what I want to do is for the where value to check all values of the combobox.

Can someone offer advice as to how this would be done?

My sql query is this at the minute:

Dim sqlOpServ As String = ("SELECT DISTINCT col1, col2 from table1, table2 where test=test1 AND value= '" & combobox1.SelectedItem & "' ORDER BY col1 ASC")

Thanks

2 Answers 2

1

I don't have VS right know so I I will try you help you without trying my code:

 Dim sWhere as string=""
 For i = 0 To ComboBox1.Items.Count - 1
        sSelect = sSelect & " AND value='" & ComboBox1.Items(i) & "' "
 Next

 Dim sqlOpServ As String = ("SELECT DISTINCT col1, col2 from table1, table2 where test=test1 " & sWhere & " ORDER BY col1 ASC")

After the comments:

   Dim sWhere As String = ""

    For i = 0 To ComboBox1.Items.Count - 1
        If i = 0 Then
            sWhere = " AND value='" & ComboBox1.Items(i).ToString & "' "
        Else
            sWhere = sWhere & " OR value='" & ComboBox1.Items(i).ToString & "' "
        End If

    Next

    Dim sqlOpServ As String = _
        ("SELECT DISTINCT col1, col2 from table1, table2 where test=test1 " & sWhere & " ORDER BY col1 ASC")

If you have used a datatable to fill your combobox

    Dim table As DataTable = DirectCast(Me.ComboBox1.DataSource, DataTable)
    Dim sWhere As String = ""

    For i = 0 To ComboBox1.Items.Count - 1
        Dim displayItem As String = table.Rows(i)(ComboBox1.DisplayMember).ToString()
        ' Dim valueItem As String = table.Rows(i)(ComboBox1.ValueMember).ToString() 'if you need at some point the value you can use this

        If i = 0 Then
            sWhere = " AND value='" & displayItem & "' "
        Else
            sWhere = sWhere & " OR value='" & displayItem & "' "
        End If

    Next

    Dim sqlOpServ As String = _
        ("SELECT DISTINCT col1, col2 from table1, table2 where test=test1 " & sWhere & " ORDER BY col1 ASC")

I hope I helped

Sign up to request clarification or add additional context in comments.

1 Comment

This will return nothing as you are saying AND ColumnName = 'VALUE1' AND ColumnName = 'VALUE2' etc. It cannot have 2 values simultaneously. You need to bracket it and put ColumnName = 'VALUE1' OR etc...
0

2 approaches

1) you can make all the combo box item values and make it separated by special characters and you can split all the values in an Stored Procedure and get the result

2) You can make a string separated by "or" ie value=22 or value=25 and you may then execute the values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.