Yerry Antonius
Yerry Antonius

Reputation: 61

Use SQL parameter to filter multiple comboboxes in vb.net

How can I filter multiple combo boxes value in vb.net with SQL server query? All the comboboxes are user input. Example:

I have cboName.text, cboAge.text, cboAddress.text

SELECT name, age, address
FROM tbMaster 
WHERE name = '" & cboName.text & "'
AND age = '" & cboAge.text & "'
AND address = '" & cboAddress.text & "'

If cboName is empty then it will display all Name.

What I did is:

IF cboName.text  = '' AND cboAge.text = '' THEN
    SELECT name,age,address FROM tbMaster

ELSEIFcboName.text <> '' AND cboAge.text = '' THEN
    SELECT name,age,address FROM tbMaster WHERE name = '" & cboName.text & "'

ELSEIF cboName.text  = '' AND cboAge.text <> '' THEN
    SELECT name,age,address FROM tbMaster WHERE age = '" & cboAge.text & "'

ELSEIFcboName.text <> '' AND cboAge.text <> '' THEN
    SELECT name,age,address FROM tbMaster WHERE name = '" & cboName.text & "' AND age = '" & cboAge.text & "'

and so on... Imagine if I have to filter 10 comboboxes.

How can I do it with SQL parameter effectively?

Thanks.

Upvotes: 0

Views: 53

Answers (0)

Related Questions