none
An expression of non-boolean type specified in a context where a condition is expected, near ','. in c#

    Question


  •         public static DataTable GetName(string searchString)
            {
                string sql = string.Format("SELECT * FROM nameAddress WHERE firstName, lastName LIKE '*" + searchString + "*' ORDER BY firstName, lastName");
                SqlDataAdapter eceAdapter = new SqlDataAdapter(sql, eceAppConnection);
                DataTable eceTable = new DataTable();

                try
                {
                    eceAdapter.Fill(eceTable);
                    return eceTable;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

     

    The error is: An expression of non-boolean type specified in a context where a condition is expected, near ','.

    Thursday, April 21, 2011 3:15 AM

Answers

  • Your SQL statement is wrong. It should be

    "SELECT * FROM nameAddress WHERE LastName LIKE '%' + @searchString + '%' ORDER BY firstName, lastName");
    

    And use @SearchString as a parameter.

    Also, this will not perform well. If you're only looking for last name starting with the specified string, then remove % in front, and it will result in a better performance if you have an index on the LastName field.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog
    • Marked as answer by Ai-hua Qiu Friday, April 29, 2011 5:25 AM
    Thursday, April 21, 2011 3:32 AM
    Moderator

All replies

  • Your SQL statement is wrong. It should be

    "SELECT * FROM nameAddress WHERE LastName LIKE '%' + @searchString + '%' ORDER BY firstName, lastName");
    

    And use @SearchString as a parameter.

    Also, this will not perform well. If you're only looking for last name starting with the specified string, then remove % in front, and it will result in a better performance if you have an index on the LastName field.


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog
    • Marked as answer by Ai-hua Qiu Friday, April 29, 2011 5:25 AM
    Thursday, April 21, 2011 3:32 AM
    Moderator
  • Check your WHERE clause:

     

    FirstName, LastName like Something..

    Here FirstName is not a condition and the comma is not a boolean operator..

    You have to write somethis like:

     

    FirstName like '%Anything%' AND LastName like '%somethingElse%'

    FirstName like '%Anything%' OR LastName like '%somethingElse%'

     

    Also note that in SQL server the wildcard is % not *

    In short, the whole WHERE clause should be calculated to TRUE or FALSE


    Simpler solution is the stronger solution
    If you find this post answers you please mark it as answer..
    If the post is useful please vote it as useful..
    Thursday, April 21, 2011 3:45 AM