Click here to Skip to main content
16,004,647 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)
        {
           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, Repair_id, New_Old_Part, Provider_id, Installation_id, Notes) values (@Items_No, @Job_Card, @Value_Part,@Auto_Parts_id , @Repair_id,@New_Old_Part , @Provider_id, @Installation_id , @Notes)";
                            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("@New_Old_Part", dgv_Add_job_card.Rows[i].Cells[5].Value);
                            sd.Parameters.AddWithValue("@Notes", dgv_Add_job_card.Rows[i].Cells[7].Value);
                            sd.Parameters.AddWithValue("@Auto_Parts_id", dgv_Add_job_card.Rows[i].Cells[9].Value);
                            sd.Parameters.AddWithValue("@Repair_id", dgv_Add_job_card.Rows[i].Cells[10].Value);
                            sd.Parameters.AddWithValue("@Provider_id", dgv_Add_job_card.Rows[i].Cells[11].Value);
                            sd.Parameters.AddWithValue("@Installation_id", dgv_Add_job_card.Rows[i].Cells[12].Value);
                            sd.ExecuteNonQuery();
                            con.Close();
                            
                        }
}



this error happened when i change code that retrieve data in combobox :-

C#
void FillComboBoxRepair()
       {
           try
           {
               con.Open();
               s = "select * from Repair_Type";
               sd = new SqlCommand(s,con);
               sda = sd.ExecuteReader();
               while(sda.Read())
               {
                   string Repair_type = sda.GetString(1);
                   CB_Repair_type.Items.Add(Repair_type);
               }
               con.Close();
           }
           catch(Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

       void FillComboBoxProviders()
       {
           try
           {
               con.Open();
               s = "select * from Providers";
               sd = new SqlCommand(s, con);
               sda = sd.ExecuteReader();
               while (sda.Read())
               {
                   string Provider_desc = sda.GetString(2);
                   CB_Providers_desc.Items.Add(Provider_desc);
               }
               con.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
Posted

The error means that you are trying to insert a value into the "Job card" column that doesn't exist in the Auto_Parts table.
Because you have a Foreign Key set up, you can only insert a value if there is a matching entry in the other table, to prevent your data being inconsistent.
Either add the row to the Auto_parts table first, or use the debugger to work out what value you are trying to insert, then find out where that came from.

We can't do this for you - we don't have any access to your data!
 
Share this answer
 
The way a foreign key works is it cannot have a value in that column that is not also in the primary key column of the referenced table.
Thus, you need to first insert a value in the primary key (table) and then insert into the child tables.
 
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