0

So here is my problem: I have a site where I have to search through the stuff on it. This is my sql sentence:

public DataTable Search(string Keyword)
{
    return db.GetData("SELECT * FROM tblBehandlinger WHERE fldYdelse LIKE @1", 
                  "%" + Keyword + "%");
}

But as you can see it will only search through the table name "fldYdelse". And I works fine, but my problem is that it wont search through two things. This I how I want it to be:

public DataTable Search(string Keyword)
{
    return db.GetData("SELECT * FROM tblBehandlinger WHERE fldYdelse LIKE 
                       @1 OR fldPris @2", "%" + Keyword + "%");
}

This is my backend of the search.aspx site:

string keyword = Request.QueryString["search"].ToString();

foreach (DataRow item in s.Search(keyword).Rows)
{
    //BLAH BLAH BLAH
}

But I can't list it out.

0

3 Answers 3

2

You have to write the LIKE predicate with the second parameter fldPris as well. Your sql statement should look like:

SELECT * 
FROM tblBehandlinger 
WHERE fldYdelse LIKE @1 
   OR fldPris   LIKE @2

Instead of:

SELECT * 
FROM tblBehandlinger 
WHERE fldYdelse LIKE @1 
   OR fldPris @2
Sign up to request clarification or add additional context in comments.

Comments

0

Repeating a parameter (string) in C# is cheap... did you want to use a single parameter twice?

return db.GetData("SELECT * FROM tblBehandlinger " +
                  "WHERE fldYdelse LIKE @1 OR fldPris LIKE @2",
                  "%" + Keyword + "%", "%" + Keyword + "%");

Or did you really miss the 2nd LIKE?

2 Comments

Huh.. didnt think of that. But thank you very much! It worked :)
Thanks for the confirmation. I think you need to wait 15 minutes before accepting the answer. Welcome to StackOverflow.
0

I think you mean

return db.GetData("SELECT * FROM tblBehandlinger WHERE fldYdelse LIKE 
                   @1 OR fldPris LIKE @1", "%" + Keyword + "%");

It's the same keyword

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.