Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I have this search problem with such error, Help?
(I use visual studio ultimate 2013 & SQL Server 2014 Management Studio):

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near the keyword 'Like'.

This is my SEARCH function:

C#
public static void SEARCH(DataGridView dtg, int I)
        {

            String SQL = "";
            UInt64 Where = 0;
            SQL = " Select  nam as[Name], fam as[Family name], fat as[Father name], nat[ID code], cel as[Cell.], tel as[Tell.], [add] as[Address] From newuser ";
            if (I == 0)
            {
                SQL = SQL + " where ";
            }
            if (I != 0)
            {
                if (Where == 0)
                {
                    Where = 1;
                }
                else
                {
                    SQL = SQL + " And ";
                }
                SQL = SQL + "I Like %" + I + "% ";

            }



            dtg.DataSource = DataBase.ExecuteSelect(SQL);



        }

And this is how I return it in my search button:

 int ID;

            ID = 0;
            ID = Convert.ToInt32(txtuserID.Text);


            newuser.SEARCH(dtgfs, ID);


What I have tried:

well I not sure what to do so I would if you can help me.
(I use visual studio ultimate 2013 & SQL Server 2014 Management Studio).
Posted
Updated 5-Aug-19 21:27pm
v2
Comments
Richard MacCutchan 6-Aug-19 3:24am    
Check the contents of the final SQL string to see what is missing or mis-quoted after the Like clause. Also what is the data type of the I field in the database?

1 solution

First off, Like is expecting a string, so you would need to send this to SQL:
SQL
SELECT ... WHERE MyColumn LIKE '%matchthis%'
To get rid of your error.
However, that looks very wrong: if I is zero, your SQL will end up as:
SQL
SELECT ... WHERE
with no condition, and if it isn't you will get either
SQL
SELECT ... I LIKE ...
Which is missing the WHERE or
SQL
SELECT ... AND I LIKE ...
Which is missing the WHERE and the initial condition!
I'd strongly suggest you learn to use the debugger and look directly at the strings you are generating, because they aren't going to work...


But ... Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900