Reputation: 73
Can something like this can be done?
"SELECT * FROM account WHERE if(@dropdownsearch) = 'ID' then id=@id else name=@name"
because I can't if else outside the query string maybe because of this:
cmd.Parameters.AddRange(queryParams.ToArray());
please help
Upvotes: 0
Views: 365
Reputation: 61
If I understand your dilemma correctly, you have a dropdown where a user may select whether to search on ID or Name. Now you need to pass one or the other in, not both, correct? I have done this before, with multiple dropdown options, using the following logic:
SELECT * FROM account
WHERE
(CASE WHEN @dropdownsearch = 'ID' then @id else ID END) = ID,
(CASE WHEN @dropdownsearch = 'Name' then @name else name END) = name
So what happens here is if they have selected ID then it will compare the value in your @id parameter to that of ID. At the same breath it will compare name to itself. (Name)Hanzou = (Name)Hanzou is always true, so every value in name will pass and @name will be ignored. The reverse works if the user selected 'Name' instead of 'ID'
Upvotes: 0
Reputation: 93694
use AND/OR
logic
SELECT *
FROM account
WHERE (@dropdownsearch = 'ID' and id=@id)
or name=@name
Upvotes: 4