Creating dynamic SQL SELECT statement
My web page is layout out with 3 select boxes, the user will select(or not) an option from each box and then click the search button.
The issue here is, how do I create an SQL SELECT statement which will be based on which options the user selects?
Full of Kramy goodness
You will have to run a few if statements to determine which options the user has selected.
if selectBoxOne.selectedItem = "value"
//' Statement sequence.
//' Alternative statement sequence.
Also note that it can get a bit hairy depending on what their selections are supposed to do. If they're intended for the WHERE clause, you need to deal with the possibility that they won't select anything, in which case you need to be sure not to leave a naked WHERE sitting around. An easy way to deal with it (if all the options will use AND logic) is to end the static portion of the query with "WHERE 1 = 1" and then you can append as many or as few options on the end in the form " AND whatever_option = 'whatever_value'" as you want without needing to worry about dealing with no options selected or handling the first selected option specially.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)