Wednesday, March 21, 2012

Problem with %....% in the Parameterized Queries

I am using MS SQL 2000.I am writing a simple search statement in the stored procedure which goes like this
Create Procedure.[dbo].[SelectSearch]
(
@.searchtext nvarchar(100)
)
SELECT * FROM TableName WHERE ColumnName LIKE @.searchtext

i want to enter the value in place of @.searchtext as '%strsearch%'
and i'm trying to enter the value of @.searchtext using Parameterized Queries like this

objcmd = new SqlCommand ("SelectSearch", objConn);
objcmd.CommandType = CommandType.StoredProcedure;
objcmd.Parameters.Add("@.searchtext", "'%" + strsearch + "%'");

but its not working out and giving the error message as
Procedure SelectSearch has no parameters and arguments were supplied

Is this the correct way to add the parameters ? Or
How to add' % on both sides of strsearch thro parameterized queries
Thank you very much in advance


You could simply append the %'s to the value.
strsearch = "%" + strsearch + "%";
objcmd = new SqlCommand ("SelectSearch", objConn);
objcmd.CommandType = CommandType.StoredProcedure;
objcmd.Parameters.Add("@.searchtext" );
Also I'd recommend adding the size of the paremeter to avoid problems later on.
|||

Thank you very much mate .

I was struggling to solve this problem atlast i got the answer . Thnx once again

No comments:

Post a Comment