-1

MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'not=41, durum='BASARISIZ' Where id=2' at line 1'

    int i = 0;
    private void button2_Click(object sender, EventArgs e)
    {
        int not = Convert.ToInt16(comboBox5.Text) + Convert.ToInt16(comboBox3.Text) + Convert.ToInt16(comboBox4.Text) + Convert.ToInt16(comboBox6.Text) + Convert.ToInt16(comboBox7.Text) + Convert.ToInt16(comboBox8.Text) + Convert.ToInt16(comboBox9.Text) + Convert.ToInt16(comboBox10.Text) + Convert.ToInt16(comboBox11.Text) + Convert.ToInt16(comboBox12.Text) + Convert.ToInt16(comboBox13.Text) + Convert.ToInt16(comboBox14.Text) + Convert.ToInt16(comboBox15.Text) + Convert.ToInt16(comboBox16.Text) + Convert.ToInt16(comboBox17.Text) + Convert.ToInt16(comboBox18.Text) + Convert.ToInt16(comboBox19.Text) + Convert.ToInt16(comboBox20.Text) + Convert.ToInt16(comboBox21.Text);


        conn.Open();
        string update = "Update ogrenci Set not=@not, durum=@durum Where id=@id";
        cmd = new MySqlCommand(update, conn);

        cmd.Parameters.AddWithValue("@not", not);
        cmd.Parameters.AddWithValue("@durum", comboBox22.Text);
        cmd.Parameters.AddWithValue("@id", dataGridView1.Rows[i].Cells[0].Value);

        cmd.ExecuteNonQuery();

        MessageBox.Show("Başarıyla Güncellendi.");

        conn.Close();

        getOgrenci();
    }
    private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        i = e.RowIndex;
        textBox1.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
        textBox4.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
        textBox2.Text = dataGridView1.Rows[i].Cells[6].Value.ToString();
    }

there is my code so i don't understand why i am getting syntax error while i don't have any syntax issue.

  • i tried to get table name i mean ogrenci between ` ` these things but it didn't worked – Samet Yılmaz May 09 '22 at 20:39
  • do you have to escape the value not with "? – JBatstone May 09 '22 at 20:48
  • 1
    `not` is a reserved keyword in every RDBMS. You'll need to throw some backticks around that column when you reference it to avoid errors. – JNevill May 09 '22 at 20:51
  • @JNevill thanks i didn't know that `not` was a reserved keyword. Thanks again. – Samet Yılmaz May 09 '22 at 20:58
  • 1
    Does this answer your question? [How do I escape reserved words used as column names? MySQL/Create Table](https://stackoverflow.com/questions/2889871/how-do-i-escape-reserved-words-used-as-column-names-mysql-create-table) – Charlieface May 09 '22 at 21:35
  • Side point: you need to dispose your connection and command objects with `using`. Do not block the thread with a message box while the connection is open – Charlieface May 09 '22 at 21:36
  • If the problem is 'an error in your SQL syntax' the solution is clearly to review and study SQL syntax including reserved and forbidden words – Ňɏssa Pøngjǣrdenlarp May 09 '22 at 21:41

1 Answers1

5

not is a reserved word in MySQL so it must be escaped with back ticks

Derek-B
  • 66
  • 3