Click here to Skip to main content
15,666,183 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.'

What I have tried:

if (textBox1.Text != "")
                Query += "and Serial_Number='" + textBox1.Text + "'";

            if (bPersianCalenderTextBox2.Text != "")
                Query += "and Received_Date = '" + bPersianCalenderTextBox2.Text + "'";

            if (bPersianCalenderTextBox1.Text != "")
                Query += "and Entry_Date = '" + bPersianCalenderTextBox1.Text + "'";

            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter();
            adp.SelectCommand = new SqlCommand();
            adp.SelectCommand.Connection = con;
            adp.SelectCommand.CommandText = "select * from tblSaleServices where 1=1 and Entry_Date != '' and Repair_Date != '' and Received_Date !=''" + Query;
            adp.Fill(ds, "tblSaleServices");
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "tblSaleServices";
Updated 3-Aug-22 9:15am
PIEBALDconsult 3-Aug-22 14:54pm    
!=''" + Query;

Don't do it like that! 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:
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:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
A perfectly valid "delete the table" command
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?

Fix that throughout your whole app and you will almost certainly find that your problem has disappeared at the same time.
Share this answer
System.Data.SqlClient.SqlException: 'Incorrect syntax near '='.'

The problem is that nobody here can know what is the real SQL query, because it depend on user input.
adp.SelectCommand.Connection = con;
// Here, print the 'CommandText' that you build in next line
adp.SelectCommand.CommandText = "select * from tblSaleServices where 1=1 and Entry_Date != '' and Repair_Date != '' and Received_Date !=''" + Query;

Then you will know what is the exact query you have. (Add this print to the question.

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
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