locked
Adding record to a table with a foreign key relationship RRS feed

  • Question

  • hello,
    i am presently pulling my hair out just trying to get this stuff to work.

    i have two tables: StaffTB and WorkLogTB with both having PK and FK relationship, respectively.
    if you run the code the below is what you did see on the screen which is exactly how i want it
    to be (one to many. in other words one employee many work days. The integer values represent the
    number of trucks a particular employee worked on and the date the work was done. so what my application
    does is to add these information from time to time for an employee who comes to work.

    The line that says: "oWorkLogTB.EmployeeID = 5" where 5 represents the employee's ID can repeat itself
    every other day to mean a particular emplyee and this is where the problem is coming from. I can't seem
    to duplicate the employees ID value--and i actually need to because the table needs to.

    Fashola Augustin:
            11    1/10/2014
            13    3/10/2014
            7     9/10/2014
    Gabriel John:
            3     1/10/2014
            11   5/10/2014
            9     9/10/2014
    Gbolawon Ahmed:
            15    1/10/2014
            11    3/10/2014
            13    5/10/2014
    Johanson Paul:
            10    3/10/2014
    Joseph Clement:
            10    5/10/2014
            5     7/10/2014
            9     9/10/2014
    Omoro Emmanuel:
            10    1/10/2014
            15    3/10/2014
            13    5/10/2014
            10    7/10/2014
            12    9/10/2014

    But the problem i am having is how to add a record to the table(WorkLogTB) which has the
    foreign key, programmatically without getting the "DuplicateKeyException". So far this is my code.
    What should i be doing that i am not doing?

    private void buttonAddRecord_Click(object sender, EventArgs e) { try { // create workLog table WorkLogTB oWorkLogTB = new WorkLogTB(); // assign values to WorkLogTB feilds oWorkLogTB.EmployeeID = 5; oWorkLogTB.NumberOfTrucks = 15; oWorkLogTB.Date = DateTime.Today; // commit changes to database DBContext.WorkLogTB.InsertOnSubmit(oWorkLogTB); DBContext.SubmitChanges(); } catch (System.Data.Linq.DuplicateKeyException dke) { MessageBox.Show(dke.Message); } }


    Wednesday, November 5, 2014 1:44 PM

Answers

  • Hello,

    >> I can't seem to duplicate the employees ID value--and i actually need to because the table needs to.

    For this, I do not quite understand, please provide a detail description.

    >>But the problem i am having is how to add a record to the table(WorkLogTB) which has the foreign key, programmatically without getting the "DuplicateKeyException".

    Could you please share your table schema with us?

    Currently, I am confused why it would throw the DuplicateKeyException because from your description, the EmployeeID should be considered to be a foreign key in the workLog table and a foreign key could be duplicated in database Or do you make the EmployID both primarykey and foreign key in worklogtable? If it is, please add a special primary key as “WorkLogID” and just let the EmployeeID be a foreign key so that you could duplicate the employees ID value

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Friday, November 14, 2014 9:30 AM
    Thursday, November 6, 2014 6:42 AM

All replies

  • Hello,

    >> I can't seem to duplicate the employees ID value--and i actually need to because the table needs to.

    For this, I do not quite understand, please provide a detail description.

    >>But the problem i am having is how to add a record to the table(WorkLogTB) which has the foreign key, programmatically without getting the "DuplicateKeyException".

    Could you please share your table schema with us?

    Currently, I am confused why it would throw the DuplicateKeyException because from your description, the EmployeeID should be considered to be a foreign key in the workLog table and a foreign key could be duplicated in database Or do you make the EmployID both primarykey and foreign key in worklogtable? If it is, please add a special primary key as “WorkLogID” and just let the EmployeeID be a foreign key so that you could duplicate the employees ID value

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Friday, November 14, 2014 9:30 AM
    Thursday, November 6, 2014 6:42 AM
  • Thanks a million for your reply as you were on point with the solution you proffered.  The problem was that i did not create a native primary key column in my WorkLogTB. Thanks for pointing that out. Problem solved!
    Wednesday, November 19, 2014 1:26 PM