Dmetrey
Dmetrey

Reputation: 29

Error parsing query: [ Token line number = 1,Token line offset = 27,Token in error = BY ]

I'm trying to pass a SQL command to delete data which has a property BY ( unique nvarchar), but I get this parsing error every time.

Error parsing query: [ Token line number = 1,Token line offset = 27,Token in error = BY ]

I have also tried to do the same command to a different property called Category (which is also nvarchar but NOT unique and not key) and the command passes and the line containing the entered string does get removed. I am thinking if it could be because I have the BY set to Unique, No NULL, KEY.. or maybe something else. Here is my simple code for the button to execute the command.

    public SqlCeConnection cn = new SqlCeConnection(@"Data Source=GAI_Database.sdf");

    private void button1_Click(object sender, EventArgs e)
    {
        cn.Open();
        SqlCeCommand cm1 = new SqlCeCommand("DELETE FROM Drivers WHERE BY = @BY", cn);
        //cm1.Parameters.AddWithValue("@BY", textBox1.Text);
        cm1.Parameters.Add("@BY", System.Data.SqlDbType.NVarChar).Value = textBox1.Text;


        try
        {
            cm1.ExecuteNonQuery();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        cn.Close();
        this.driversTableAdapter.Fill(this.gAI_DatabaseDataSet.Drivers);
    }

Upvotes: 1

Views: 665

Answers (3)

Tiago Guedes
Tiago Guedes

Reputation: 85

BY is a reserved word in every database. Try

DELETE FROM Drivers WHERE [BY] = @BY

Upvotes: 1

sgeddes
sgeddes

Reputation: 62831

I think BY is a reserved word. Try escaping it with brackets.

DELETE FROM Drivers WHERE [BY] = @BY

Upvotes: 1

Dan
Dan

Reputation: 10680

BY is a reserved word in most SQL dialects.

If you're referring to a column in your Drivers table, try to enclose it with apostrophes or brackets, as in:

DELETE FROM Drivers WHERE [BY] = @BY

or possibly:

DELETE FROM Drivers WHERE ´BY´ = @BY

depending on which RDBMS you're using.

Upvotes: 1

Related Questions