Violation of PRIMARY KEY constraint SQlServer

enter image description here

enter image description here

string query = "Update Completed_orders set service=N'" + comboBox1.SelectedValue + "',kolvo=N'" + kol.Text + "',note=N'" + not.Text + "' where orders='" + form3.ordersgrid.CurrentRow.Cells[0].Value.ToString() + "' ";
SqlCommand cmd = new SqlCommand(query, dataBase.getConnection());
dataBase.openConnection();
cmd.ExecuteNonQuery();
dataBase.closeConnection();
form3.serviceklient();
MessageBox.Show("Изменена");
this.Hide();

Вот ошибка:

System.Data.SqlClient.SqlException: "Violation of PRIMARY KEY constraint 'PK__tmp_ms_x__1A00DC411E67C062'. Cannot insert duplicate key in object 'dbo.Completed_orders'. The duplicate key value is (23, 1). The statement has been terminated."

Ругается на UPDATE Новый код:

 string query = "Update Completed_orders set service=N'" + comboBox1.SelectedValue + "',kolvo=N'" + kol.Text + "',note=N'" + not.Text + "' where orders='" + form3.ordersgrid.CurrentRow.Cells[0].Value.ToString() + "' and service='" + form3.ordersgrid.CurrentRow.Cells[0].Value.ToString() + "' ";
        SqlCommand cmd = new SqlCommand(query, dataBase.getConnection());
        dataBase.openConnection();
        cmd.ExecuteNonQuery();
        dataBase.closeConnection();
        form3.serviceklient();
        MessageBox.Show("Изменена");
        this.Hide();

Ответы (1 шт):

Автор решения: Alex Varyushin

У тебя составной первичный ключ видимо из Id заказа и количества уже существует запись на заказ 23 с количеством 1, вот он и не может тебе сделать такой же

измени первичный ключ

ALTER TABLE dbo.Completed_orders
  DROP PRIMARY KEY;

ALTER TABLE dbo.Completed_orders
   ADD ID INT IDENTITY
       CONSTRAINT PK_YourTable PRIMARY KEY CLUSTERED
→ Ссылка