JT_SIRO
asked on
ASP.NET SelectCommand Syntax Question
I'm trying to add a simple keywords search to my ojbect datasource and am having trouble with the syntax. I basically want to say 'select * from xyz where Description like '%The keywords from textbox%'. How do I represent the "%" character in my SelectCommand?
It works fine when I put: Description = @Keywords, but I need the "%"s.
I tried the following which didn't work:
"Description like '%@Keywords%'"
Here's my complete SelectCommand:
SelectCommand="SELECT [RecID], [TrackTitle], [Composer], [Filename], [Popularity], [Time], [Description], [Category], [SubCategory], [Keywords], [BPM], [CDTitle], [IsVocal] FROM [metadata] WHERE Composer like @Artist AND Status = @Status AND Description like @Keywords"
It works fine when I put: Description = @Keywords, but I need the "%"s.
I tried the following which didn't work:
"Description like '%@Keywords%'"
Here's my complete SelectCommand:
SelectCommand="SELECT [RecID], [TrackTitle], [Composer], [Filename], [Popularity], [Time], [Description], [Category], [SubCategory], [Keywords], [BPM], [CDTitle], [IsVocal] FROM [metadata] WHERE Composer like @Artist AND Status = @Status AND Description like @Keywords"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oops my bad...forgot to refresh...
ASKER
Great, that worked! Now how would account for the user leaving the keywords field blank. With this syntax "AND [Description] like '%' + @Keywords + '%'", it requires that there be something in Keywords. How would I add some "if not null" clause?
ASKER
FYI - I certainly know how to do this in code behind, but I'd like to utilize the object datasource. I'm using it to save time, and it's wasting me time.... Uggg.
If the @Keywords parameter is an empty string, you can just leave it like it is, and it will produce the string '%%' which matches anything.
If the @Keywords parameter can be null, use the isnull function:
AND [Description] like '%' + isnull(@Keywords, '') + '%'
If the @Keywords parameter can be null, use the isnull function:
AND [Description] like '%' + isnull(@Keywords, '') + '%'
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try this..
store @keywords in a variable, and than assign the variable in the selectcommand
store in a variable..
string searchString = "%" + @keywords + "%"
assign it..
"AND Description like '" + searchString + "'"
store @keywords in a variable, and than assign the variable in the selectcommand
store in a variable..
string searchString = "%" + @keywords + "%"
assign it..
"AND Description like '" + searchString + "'"
ASKER
Thanks everyone!
... AND [Description] like @Keywords