I want to update my relation table between book and user named R_Kital_Al
But I get this error ;
System.Data.SqlClient.SqlException (0x80131904): Cannot insert duplicate key row in object 'dbo.R_Kitap_Al' with unique index 'IX_Kitap_Al'. The duplicate key value is (15).
How can I fix this error?
private void Update_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(@"Data Source=HP\SQLEXPRESS;Initial Catalog=Kütüphane;Integrated Security=True");
conn.Open();
SqlCommand cmd = new SqlCommand("UPDATE R_Kitap_Al SET SSN=@SSN,Book_No=@Book_No,BaslamaTarihi=@BaslamaTarihi,BitisTarihi=@BitisTarihi", conn);
DateTime dt = new DateTime();
cmd.Parameters.AddWithValue("@SSN",dataGridView1.CurrentRow.Cells[0].Value);
cmd.Parameters.AddWithValue("@Book_No",book_NoTextBox.Text);
cmd.Parameters.AddWithValue("@BaslamaTarihi",baslamaTarihiDateTimePicker.Value);
dt = Convert.ToDateTime(baslamaTarihiDateTimePicker.Value);
dt = dt.AddDays(0);
bitisTarihiDateTimePicker.Value = dt.AddDays(15);
cmd.Parameters.AddWithValue("@BitisTarihi", bitisTarihiDateTimePicker.Value);
cmd.ExecuteNonQuery();
this.Hide();
R_Kitap_Al kit = new R_Kitap_Al();
kit.ShowDialog();
this.Close();
}
There is no where clause so you are updating every row in that table with the specified values. Is one of those columns a primary or foreign key?
We cannot insert duplicate value in unique Unique Key.
So, In your table 'SSN' or 'Book_No ' column might be Unique. Please check '15' exists in column 'SSN' or 'Book_No'
Please remove unique key from mention column or try to update with different value rather than 15.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.