LIKE % question

Hello,

I'm working on a project where I have a drop-down menu (collectionlist) of different words, which are part of a title which is located in my database. The bolded line is where I'm having my problems. I'm not sure how to place the LIKE statement.

I will give you a quick example of what I would like to do:

In the CollectionList, there is the word head. In my database, there is the title, head with earrings. I would like the filter function to find head with earrings when looking for the region, head.

:S

Thanks, I know this is a simple one, but I can't seem to find where I'm going wrong.

Here's the code:

---------------------------------------------------------------------------

string strSQL = "SELECT ca_id, ca_titel, ca_bildurl1, ca_alter, ca_sex " +

"FROM fragenkatalog";

string strSQLFilter = "";

if ( (int)regioncombo.SelectedIndex > 0 )

{

if ( strSQLFilter.Length > 0 )

{

strSQLFilter += " AND ";

}

strSQLFilter = "ca_titel = '" + regioncombo.SelectedItem.ToString() + " LIKE ca_titel%" + "'";

}

if ( strSQLFilter.Length > 0 )

{

strSQL += " WHERE " + strSQLFilter + " ORDER BY ca_titel";

}

LoadList(strSQL);



Answer this question

LIKE % question

  • asifbhura

    could you explain about the problem at little more , and i think there is a little error in your sql query string, like using AND after WHERE


  • AlkPonn

    Well, I tried it like it is, and it seems that all "heads" are found, yet the age and gender are basically ignored.


  • van Kooten

    yes, you are right , and your query will run correctly and find both "head with hearing aid" and "head with earrings".


  • SPIDEY25

    Ali Raza Shaikh wrote:

    Correct the following statement, there should be a + before =

    strSQLFilter += "ca_titel LIKE '" + regioncombo.SelectedItem.ToString() + "%'";

    Hahaha, thanks, Ali. That would have taken me forever to find out myself. You have been very, very helpful to me. Thank you very much. I have just deployed the program and it works!

    Thanks again!

    Martina


  • bertuahki

    Sure,

    The problem is that I have 3 search criteria. 2 of the 3 are exact matches (m/f, 1-4), but the third search criterium is a region (hence, regioncombo). The region is part of a title, as I mentioned above.

    For example, in my CollectionList, there is "head" and in the database (SQL) there is "head with hearing aid". I am hoping the filter would find "head with hearing aid" by the region "head."

    So, if I expand my filter criteria, by saying: head, 3, and female, that I would find the 3-year old female with the title "head with hearing aid". Possibly, however, the 3-year old female with "head with earrings" would be found too. These results would then shown in a ListView box.

    Like I said, this works for the 1-4 and m/f drop-downs.

    Is this what you needed to know


  • PC-Gram

    Correct the following statement, there should be a + before =

    strSQLFilter += "ca_titel LIKE '" + regioncombo.SelectedItem.ToString() + "%'";



  • Gabeh

    try this

    strSQLFilter = "ca_titel LIKE '" + regioncombo.SelectedItem.ToString() + "%'";


  • Bradonline

    Thank you!

    The problem is, I have other search criteria (age, etc), which is then not found...

    Any ideas

    string strSQL = "SELECT ca_id, ca_titel, ca_bildurl1, ca_alter, ca_sex " +

    "FROM fragenkatalog";

    string strSQLFilter = "";

    if ( (int)gendcombo.SelectedIndex > 0 )

    {

    if ( strSQLFilter.Length > 0 )

    {

    strSQLFilter += " AND ";

    }

    strSQLFilter = "ca_sex = '" + gendcombo.SelectedItem.ToString() + "'";

    }

    if ( (int)agecombo.SelectedIndex > 0 )

    {

    if ( strSQLFilter.Length > 0 )

    {

    strSQLFilter += " AND ";

    }

    strSQLFilter += "ca_alter = '" + agecombo.SelectedItem.ToString() + "'";

    }

    if ( (int)regioncombo.SelectedIndex > 0 )

    {

    if ( strSQLFilter.Length > 0 )

    {

    strSQLFilter += " AND ";

    }

    strSQLFilter = "ca_titel LIKE '" + regioncombo.SelectedItem.ToString() + "%'";

    }

    if ( strSQLFilter.Length > 0 )

    {

    strSQL += " WHERE " + strSQLFilter + " ORDER BY ca_titel";

    }

    LoadList(strSQL);


  • LIKE % question