Showing posts with label parametersi. Show all posts
Showing posts with label parametersi. Show all posts

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.

Saturday, February 25, 2012

Parameters

I have a problem in SQL Reporting Services.

I have several multi-value parametersI

When I use one single value in parameter and running the report work fine, but when I select more then one value in the parameter (With Multi Value ) it fails:

"An error occurred during local report precessing. Query execution failed for dataset "DataSet" incorrect syntax near ',' "

thanks

OK, do you want us to read the whole query ? As you are more involved in the logic of the query I would suggest you turning on the profile to see what is actually fired against the SQL Server database. But as from a first view you are using multivalue parameters like singlevalue and multivalue together in @.pzona = 0 and (@.pzona in @.Pzona whatever that means)

Using the profiler will help you to find your answer to that syntax problem.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||

I have a problem in SQL Reporting Services.

I have several multi-value parametersI

When I use one single value in parameter and running the report everything work fine, but when I chose more then one value in the parameter (With Multi Value ) it fails:

"An error occurred during local report precessing. Query execution failed for dataset "DataSet" incorrect syntax near ',' "

thanks

|||

You need to make sure that your SQL query in the DataSet uses the "IN" clause instead of "=".

For ex.

Select ColumnA,ColumnB,ColumnC from MyTable

where ColumnA in (@.Parameter)

|||

That error could mean that you are trying to use the IN syntax but the parameter values are of character/string type.

If so, please see this thread -- http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1705421&SiteID=1 -- it's a little involved, but you need to split the set of multiple parameters up and put them back together with string delimiters...

>L<

Parameters

I have a problem in SQL Reporting Services.

I have several multi-value parametersI

When I use one single value in parameter and running the report work fine, but when I select more then one value in the parameter (With Multi Value ) it fails:

"An error occurred during local report precessing. Query execution failed for dataset "DataSet" incorrect syntax near ',' "

thanks

OK, do you want us to read the whole query ? As you are more involved in the logic of the query I would suggest you turning on the profile to see what is actually fired against the SQL Server database. But as from a first view you are using multivalue parameters like singlevalue and multivalue together in @.pzona = 0 and (@.pzona in @.Pzona whatever that means)

Using the profiler will help you to find your answer to that syntax problem.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

I have a problem in SQL Reporting Services.

I have several multi-value parametersI

When I use one single value in parameter and running the report everything work fine, but when I chose more then one value in the parameter (With Multi Value ) it fails:

"An error occurred during local report precessing. Query execution failed for dataset "DataSet" incorrect syntax near ',' "

thanks

|||

You need to make sure that your SQL query in the DataSet uses the "IN" clause instead of "=".

For ex.

Select ColumnA,ColumnB,ColumnC from MyTable

where ColumnA in (@.Parameter)

|||

That error could mean that you are trying to use the IN syntax but the parameter values are of character/string type.

If so, please see this thread -- http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1705421&SiteID=1 -- it's a little involved, but you need to split the set of multiple parameters up and put them back together with string delimiters...

>L<

Parameters

I have a problem in SQL Reporting Services.

I have several multi-value parametersI

When I use one single value in parameter and running the report work fine, but when I select more then one value in the parameter (With Multi Value ) it fails:

"An error occurred during local report precessing. Query execution failed for dataset "DataSet" incorrect syntax near ',' "

thanks

OK, do you want us to read the whole query ? As you are more involved in the logic of the query I would suggest you turning on the profile to see what is actually fired against the SQL Server database. But as from a first view you are using multivalue parameters like singlevalue and multivalue together in @.pzona = 0 and (@.pzona in @.Pzona whatever that means)

Using the profiler will help you to find your answer to that syntax problem.

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de|||

I have a problem in SQL Reporting Services.

I have several multi-value parametersI

When I use one single value in parameter and running the report everything work fine, but when I chose more then one value in the parameter (With Multi Value ) it fails:

"An error occurred during local report precessing. Query execution failed for dataset "DataSet" incorrect syntax near ',' "

thanks

|||

You need to make sure that your SQL query in the DataSet uses the "IN" clause instead of "=".

For ex.

Select ColumnA,ColumnB,ColumnC from MyTable

where ColumnA in (@.Parameter)

|||

That error could mean that you are trying to use the IN syntax but the parameter values are of character/string type.

If so, please see this thread -- http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1705421&SiteID=1 -- it's a little involved, but you need to split the set of multiple parameters up and put them back together with string delimiters...

>L<