Question : Need Help.. couldn't find my problem

Dear Experts,

I am facing a problem. I wrong a store procedure when i pass the value manually its working fine but when i pass the value thru the string @criteria. its not retrieving the values from the database Could you please help me to figure out where i am doing the mistake. I am inclosing the store procedure and the code as well.. Your prompt action is highly appreicated.

for more informaiton my sCriteria contains at run time the following values

here is the procedure call. where sCriteria is

"'%naz%'"

or it will be like

"'%office%' OR KeyWord_En Like '%naz%'"

where KeyWord_En is the filed name

My StoreProcedure is like this

spGetSearchEn
@Criteria nvarchar(2000)
AS
BEGIN
SET NOCOUNT ON;
SELECT ID, Name_En, Description_En  from tbldata
Where KeyWord_En Like @Criteria
END

Regards
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
public static DataSet GetSearchResultEn(string sCriteria)
        {
            string qry;
            qry = "SELECT ID, Name_En, Description_En  from tblAlbahaData Where KeyWord_En Like" + sCriteria;
            SqlParameter[] inputArray = new SqlParameter[1];
            inputArray[0] = new SqlParameter("@Criteria", SqlDbType.NVarChar,2000);
            inputArray[0].Value = sCriteria;

            try
            {
                return SqlHelper.ExecuteDataset(Utility.ConnectionString(), CommandType.StoredProcedure, "spGetSearchEn", inputArray);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

Answer : Need Help.. couldn't find my problem

>or it will be like
>"'%office%' OR KeyWord_En Like '%naz%'"

that will not work, as the t-sql will not interprete that the string actually contains any sql.
to do that, you would need dynamic sql, or read up here:
http://www.experts-exchange.com/articles/Database/Miscellaneous/delimited-list-as-parameter-what-are-the-options.html


you shall call the procedure with this argument:
"%naz%"
"%office%^%naz%"
%office%^%naz%^%test%"


and your code changes to:


spGetSearchEn
@Criteria nvarchar(2000)
AS
BEGIN
SET NOCOUNT ON;
SELECT t.ID, t.Name_En, t.Description_En  
from tbldata t
JOIN dbo.parmsToList(@Criteria, '^') l
  ON t.KeyWord_En Like l.Value
END







Random Solutions  
 
programming4us programming4us