Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following line of code want to submit value in the db but when I click On button but I get a following error.I have Made two tables in those tables EmpId Set to Primary Key And Id in other table set primary key and both are related by FK relationship.Where is problem where should I make changes.
Thanks & Regards.

SQL
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CheckInCheckOut_Employee". The conflict occurred in database "EmployeeRecord", table "dbo.Employee", column 'empId'.
The statement has been terminated.

C#
 protected void imgbtnUpadteDB(object sender, ImageClickEventArgs e)
        {
            DBClassesDataContext db = new DBClassesDataContext();
            CheckInCheckOut chk = new CheckInCheckOut();
            Employee em = new Employee();
            chk.checkTime = System.DateTime.Now;
            if (chk.checkTime != null)
            {
                chk.checkTime = DateTime.Now;
            }
            else
            {
                chk.checkoutTime = DateTime.Now;
            }
            
            empId = 1;
          
            em.name = txtCheckIn.Text;
            db.CheckInCheckOuts.InsertOnSubmit(chk);
            db.Employees.InsertOnSubmit(em);
            db.SubmitChanges();
            Response.Redirect("Default.aspx");
}
Posted

1 solution

SQL
db.CheckInCheckOuts.InsertOnSubmit(chk);
db.Employees.InsertOnSubmit(em);



 1st statement should be
 db.Employees.InsertOnSubmit(em);
 Then 
 2nd statement should be
 db.CheckInCheckOuts.InsertOnSubmit(chk);
 
Share this answer
 
Comments
M Ali Qadir 29-Feb-12 5:00am    
No,Its Yet same problem.I have changed the order but still FK conflict.

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