Skip to content
Advertisement

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);
    }

Advertisement

Answer

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

DELETE FROM Drivers WHERE [BY] = @BY
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement