views:

162

answers:

2

I have three dropdown boxes, Region, District, and City. I want my District dropdown to have a "Select All" option so the user can get all Cities in the Region, else just display the City based on the selected District. My query looks like this:

IF @district =-2 THEN 
(SELECT DISTINCT city
FROM myTable
WHERE  RIGHT(Region, 3) = ?)
ORDER BY city) 
ELSE 
(select DISTINCT city
  FROM myTable WHERE District = ?)
Order by city

I'm using vb.net/sql I couldn't find any complex case scenarios in my search either. Any sugguestions would be appreciated!

+1  A: 

I am not really sure what your question is, but note that the query can simplified as follows:

SELECT DISTINCT city
FROM myTable
WHERE (@district = -2 and RIGHT(Region, 3) = ?)
    or (@district <> -2 and District = ?)
ORDER BY city
RedFilter
+1  A: 

2 ways, either append a select statement to your SQL, or add the option in the page_load using

if(!Page.IsPostBack)
{
    DropDown1.Items.Insert(0, new ListItem("Select All", 0));

}
C Bauer
While the option should be added to the Drop Down Menu, not sure how this solves the SQL problem. It would just search for WHERE District = '0' wouldn't it?
TheDPQ
I'm really sorry to be getting back to you so late but I browse SO at work and since they implemented OpenID I haven't been able to use it (probably a proxy thing). In this situation you would hard code your SQL statement to accept 0 as 'all' using something along the lines of WHERE (@District=0 OR @District=District)
C Bauer