473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SQL LIKE query with variable

I can execute the following in SQL Query Analyzer:
Select productID, productName from Products where productName LIKE '%Spider%'
and get the results I was expecting.

I must be using the wrong syntax when I try to use a variable instead of a hard-coded search string in my ASP.NET code. I am not getting any results.
strSearch = "SELECT productID, productName from Products WHERE productName LIKE '%@searchphrase%'"
cmdSearch = New SqlCommand(strSearch, conMyData)
cmdSearch.Parameters.Add("@searchphrase", txtSearchPhrase.Text)

Thank you for any help you can give me,
Judy
Nov 18 '05 #1
2 1857
try
strSearch = "SELECT productID, productName from Products WHERE productName
LIKE '@searchphrase'"
cmdSearch = New SqlCommand(strSearch, conMyData)
cmdSearch.Parameters.Add(%@searchphrase%, txtSearchPhrase.Text)

Put %...% in the parameters.
I think it work

Jean Jacques Serpoul

"Judy Ward" <Ju******@discussions.microsoft.com> a écrit dans le message de
news:FF**********************************@microsof t.com...
I can execute the following in SQL Query Analyzer:
Select productID, productName from Products where productName LIKE '%Spider%' and get the results I was expecting.

I must be using the wrong syntax when I try to use a variable instead of a hard-coded search string in my ASP.NET code. I am not getting any results. strSearch = "SELECT productID, productName from Products WHERE productName LIKE '%@searchphrase%'" cmdSearch = New SqlCommand(strSearch, conMyData)
cmdSearch.Parameters.Add("@searchphrase", txtSearchPhrase.Text)

Thank you for any help you can give me,
Judy

Nov 18 '05 #2
That exact syntax didn't work, but gave me the idea to try this:
cmdSearch.Parameters.Add("@searchphrase", "%" & txtSearchPhrase.Text & "%")

That worked. Thank you very much for responding!
Judy

"Jean Jacques Serpoul" wrote:
try
strSearch = "SELECT productID, productName from Products WHERE productName
LIKE '@searchphrase'"
cmdSearch = New SqlCommand(strSearch, conMyData)
cmdSearch.Parameters.Add(%@searchphrase%, txtSearchPhrase.Text)

Put %...% in the parameters.
I think it work

Jean Jacques Serpoul

"Judy Ward" <Ju******@discussions.microsoft.com> a écrit dans le message de
news:FF**********************************@microsof t.com...
I can execute the following in SQL Query Analyzer:
Select productID, productName from Products where productName LIKE

'%Spider%'
and get the results I was expecting.

I must be using the wrong syntax when I try to use a variable instead of a

hard-coded search string in my ASP.NET code. I am not getting any results.
strSearch = "SELECT productID, productName from Products WHERE productName

LIKE '%@searchphrase%'"
cmdSearch = New SqlCommand(strSearch, conMyData)
cmdSearch.Parameters.Add("@searchphrase", txtSearchPhrase.Text)

Thank you for any help you can give me,
Judy


Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
by: shank | last post by:
Per a previous suggestion, I'm trying to use a parametized query in Access 2002. The query functions properly in Access. Now I'm trying to call it from ASP. I'm using code I found at...
5
by: Jason | last post by:
The following stored procedure is taking too long (in my opinion). The problem seems to be the SUM line. When commented out the query takes a second or two. When included the response time climbs...
2
by: Michelle | last post by:
Hi all, I have a query that scans huge table consists of 8 or more millions records. The funny thing is that if I use the query with local variable, the query takes more than 1 minutes,...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
4
by: icony | last post by:
Hi everyone, I have this slq code slqtext = "SELECT . From total_installs, uachoisi WHERE (((.)='*afe*'));" which makes a query that only accept values containing "afe". It works perfectly.
9
by: Kelii | last post by:
I've been trying to get this piece to work for a few hours, but have given up. I hope someone out there can help, I think the issue is relatively straightforward, but being a novice, I'm stumped....
2
by: I Hate My Computer | last post by:
I am using frames on a website. The title link on the title page adds a query string. The link goes to a page with two rows the second has two columns. I want the right column to be changed depending...
2
by: Dinesh | last post by:
Hi experts, I am working on SQL Server 2005. Now i have to write a query which will extract some information from a table. My main table is having few columns supose 3 columns. EmpID ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.