Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btn_Add_Click(object sender, EventArgs e)
{
    try
    {
        for (int i = 0; i < dgv_Add_job_card.Rows.Count ; i++)
        {
            con.Open();
            s = "insert into Auto_Part_Details (Items_No, Job_Card, Value_Part, Auto_Parts_id) values (@Items_No, @Job_Card, @Value_Part,@Auto_Parts_id )";
            sd=new SqlCommand(s,con);
            sd.Parameters.AddWithValue("@Items_No", dgv_Add_job_card.Rows[i].Cells[0].Value);
            sd.Parameters.AddWithValue("@Job_Card", dgv_Add_job_card.Rows[i].Cells[1].Value);
            sd.Parameters.AddWithValue("@Value_Part", dgv_Add_job_card.Rows[i].Cells[2].Value);
            sd.Parameters.AddWithValue("@Auto_Parts_id",dgv_Add_job_card.Rows[i].Cells[3].Value);
            sd.ExecuteNonQuery();
            con.Close();
        }
    }
Posted
Updated 19-Jan-16 22:44pm
v2
Comments
Tomas Takac 20-Jan-16 4:42am    
We need to know how the table looks like, including the PK of course. Ideally add a create script to your question (via Improve question).
Sri Nivas (Vasu) 20-Jan-16 5:45am    
Error itself telling whats the wrong is :)

1 solution

The error clearly tells you that you are inserting a value to the Id which is a Primary Key and that value already exists.

This is because Primary Key is unique and can't be duplicated. So, try inserting a unique value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900