Monday, March 12, 2012

Parameters with LIKE statement

I have a sql that I want to execute with LIKE and parameters:
I tried several options outlined that I found athttp://aspnet101.com/aspnet101/tutorials.aspx?id=10%20
but they all seem to return 0 records. When I try and execute my statement in Enterprise manager, it works fine.
Code Snippet:
...
Dim sql as string
sql = "SELECT * FROM tblName WHERE First LIKE '%' + @.fname + '%' AND Last LIKE '%' + @.lname + '%'"
Dim param(1) as sqlParameter
sqlParams(0) =New SqlParameter("@.fname", SqlDbType.VarChar, 50)
sqlParams(0).Value = Trim(fname.text)
sqlParams(1) =New SqlParameter("@.lname", SqlDbType.VarChar, 50)
sqlParams(1).Value = Trim(lname.text)
...
Can anyone tell me if there is anything wrong with my code above?Do you call Command.Parameters.Add after you set the values?
If not you must add the parameters to the Command after their values are set for example::

sqlParams(0) =New SqlParameter("@.fname", SqlDbType.VarChar, 50)
Command.Parameters.Add(sqlParams(0))

Sam


|||

Yes. I actually pass my parameter collection to a class that adds it to the command. It's a bit more complicated and so I don't want to post that part. I'm just wondering if the code section that I had posted was correct.
All my sqls work fine with parameters except when I use the LIKE clause.
Thanks.

|||Your SQL statement is fine.
Check out the way you pass parameters to ur Statement
regards|||Alternatively you could also do like this :
Dim sql as string
sql = "SELECT * FROM tblName WHERE First LIKE @.fname AND Last LIKE @.lname "
when you define the parameters and pass the value you could say:
sqlParams(0).Value = "%" & Trim(fname.text) & "%"
|||Use SQL Profile, and make sure "statement starting" is on, and see what SQL is actually being sent...also, what error are you seeing?|||

I don't see any error messages. It just returns 0 records when it should be return all records.

I simplified my code:

Dim strSQLTextAsString
strSQLText = "SELECT * FROM tbl WHERE Name LIKE @.name AND Email LIKE @.email"

Dim _conStringAsString
_conString = configurationSettings.appSettings("conString")

Dim _conSQLAs SqlConnection

Dim _SQLCommandAs SqlCommand

Try

_conSQL=New SqlConnection(_conString)
_SQLCommand =New SqlCommand
_SQLCommand.CommandText = strSQLText
_SQLCommand.Connection = _conSQL
_SQLCommand.Parameters.Add("@.name",String.Format("%{0}%", name.text))
_SQLCommand.Parameters.Add("@.email",String.Format("%{0}%", email.text))
_conSQL.Open()

Dim Data =New DataSet
Dim dataAdapter =New SqlDataAdapter(_SQLCommand)
dataAdapter.Fill(Data)

Dim dtAsNew DataTable
dt = Data.Tables(0)

Finally

_conSQL.Dispose()
_SQLCommand.Dispose()

EndTry

--
Can anyone tell me what's wrong with the code?
Thanks!!
|||Its a good idea to add the size of each of the parameters.|||

I figured out what was not working. I had created a sqlParameter array and was passing that to my data access funtion... (code in original posting). That I think was screwing up the %.

Anyhow, found a work around and is working fine now

Thanks much for everyone's pointers.

No comments:

Post a Comment