I have an application that executes a stored procedure. There is a combo box "City" and the user that can select only one value and I use that value as an SQL parameter in My C# code.
Simplified Example :
Select StudentName, Age
From dbo.Students
Where City = @city
@city is replaced by the value from the combo box.
Now the requirement is changed and the combo box supports multiple selection. The selected elements are stored in an array. Now I am using the following approach.
- Use String.Join("','") and join the array elements and the result is assigned to @city.
Changing the query to use IN operator.
Select StudentName, Age From dbo.Students Where City IN (@city)
Suppose the selected cities are London and Sydney then @City becomes London','Sydney. Then in SP it will be used as 'London','Sydney' which is syntactically correct. But this query fails.
What do I do incorrectly? What is the best (Nice) approach to handle this?