Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have four tables M_Division

Divsion_ID(primary key) Division

1 abc

2 xyz
M_Designation

Designation_ID(primary key) Designation

1 scientist

2 doctor
M_emp personal

empid(primary key) Divsion_ID(foreign key) Designation_ID(foreign key) emp_name

1 1 1 mike

2 2 2 justin
M_temp

empid(foreign key) Divsion_ID(foreign key) Designation_ID(foreign key) date purpose

1 1 1 12-06-2013 xyz

2 2 2 12-06-2013 abc

I want to Insert the foreign key ID's into the M_temp table... If I give a query like this,it gives me the foreign key constraint error.....

Insert into M_temp(EmpID,DivisionID,DesigID,date,purpose,reason,created_date) Select(EmpID,DivisionID,DesigID from M_emp_personal where Empid=@empid",conn);

purpose,date are the form fields in my ASP.NET and they need to be inserted in the M_temp along with the DivID DesigID and EmpID...thats the problem I am facing...I dont know how exactly to do this can somebody help me out?

This is the C# code
C#
protected void btnSubmit_Click2(object sender, EventArgs e)
       {
           string RelaseDate = Calendar1.SelectedDate.Date.ToString();

           SqlCommand cmd = new SqlCommand("Insert into T_TADA_tempform(EmpID,DivisionID,DesigID,date,purpose,reason,created_date) values(@EmpID,@DivisionID,@DesigID,@GPFNo,@date,@purpose,@reason), conn);
           cmd.Parameters.AddWithValue("@EmpID", ddlname.SelectedValue);
           cmd.Parameters.AddWithValue("@DivisionID", divisionID);
           cmd.Parameters.AddWithValue("@DesigID", lbldiv.Text);
           cmd.Parameters.AddWithValue("date", RelaseDate);
           cmd.Parameters.AddWithValue("@purpose", ddlpurpose.SelectedValue);
          




           if (conn.State == ConnectionState.Closed)
           {
               conn.Open();
               int cnt = cmd.ExecuteNonQuery();
               conn.Close();
               if (cnt == 1)
               {
                   Response.Redirect("");

               }
               else
                   Response.Write("Form has not been submitted,Please Try again!");
           }

       }
Posted
Comments
JoCodes 13-Oct-13 4:59am    
Can you post the full error message ?
Member 10066916 13-Oct-13 7:14am    
The exact error is Insert statement conflicted with foreign key constraint.

1 solution

It seems that you are trying to insert the child row which doesn't have in your parent tables ,thus the child-parent (ie foreign key) constraint is violated . Check empid(foreign key) Divsion_ID(foreign key) Designation_ID(foreign key) in their corresponding tables before you insert.
 
Share this answer
 
Comments
Member 10066916 13-Oct-13 7:04am    
can u elaborate a bit more?...I still didn't understand..all the values are already inserted in the M_Division,M_Designation and M_emp_personal...

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