Proper way to use LIKE operator with SqlDataSource and control parameters in declarative markup?

G

Guest

I'm trying to use LIKE operators in SqlDataSource with Control Parameters.
The following should express my intent, but it does not work in the way I
want it to (returns no results):

SelectCommand="SELECT title, conid FROM Contentitems WHERE ((categoryid =
'102') AND (byline LIKE '%@byline%')) OR ((categoryid = '102') AND (keywords
LIKE '%@keywords%')) OR ((categoryid = '102') AND (keywords LIKE
'%@jobid%')) OR ((categoryid = '102') AND (title LIKE '%@keywords%'))">

Besides generating the string that should go with the SelectCommand
programmatically, is there a way to make this work right declaratively?

Subbing in literal values returns a whole lot of results:

SelectCommand="SELECT title, conid FROM Contentitems WHERE ((categoryid =
'102') AND (byline LIKE '%president%')) OR ((categoryid = '102') AND
(keywords LIKE '%president%')) OR ((categoryid = '102') AND (jobid LIKE
'%president%')) OR ((categoryid = '102') AND (title LIKE '%president%'))">

Help out there? MSDN managed newsgroup support should be linked to this
account in, oh, 12 hours or so. :)

Thanks!

-KF
 
G

Guest

Yeah, I know that that first SQL statement seemed a little funny, but the
"corrected" version doesn't help either:

SelectCommand="SELECT title, conid FROM Contentitems WHERE ((categoryid =
'102') AND (byline LIKE '%@byline%')) OR ((categoryid = '102') AND (keywords
LIKE '%@keywords%')) OR ((categoryid = '102') AND (jobid LIKE '%@jobid%'))
OR ((categoryid = '102') AND (title LIKE '%@title%'))">
 
G

Guest

This CAN be done from declarative code.

The answer is to use concatenation operators and single quotes around the
percent symbols as follows:

SelectCommand="SELECT title, conid FROM Contentitems WHERE ((categoryid =
'102') AND (byline LIKE '%' + @byline + '%')) OR ((categoryid = '102') AND
(keywords LIKE '%' + @keywords + '%')) OR ((categoryid = '102') AND (jobid
LIKE '%' + @jobid + '%')) OR ((categoryid = '102') AND (title LIKE '%' +
@title + '%'))">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top