There are two tables. They need to enter data from the TextBox
:
class PersonData { public string FirstName { get; set; } public string LastName { get; set; } public string WorkPhone { get; set; } public string HomePhone { get; set; } Person person = new Person(); Phones phones = new Phones(); public PersonData(string firstName, string lastName, string workPhone, string homePhone) { FirstName = firstName; LastName = lastName; WorkPhone = workPhone; HomePhone = homePhone; } public void InsertData() { NoteBookDataContext noteBook = new NoteBookDataContext(); Person person = new Person(); Phones phones = new Phones(); person.FirstName = FirstName; person.LastName = LastName; phones.HomePhone = HomePhone; phones.WorkPhone = WorkPhone; noteBook.Person.InsertOnSubmit(person); noteBook.Phones.InsertOnSubmit(phones); noteBook.SubmitChanges(); } }
On the button hung handler:
private void Button_Click(object sender, RoutedEventArgs e) { PersonData personData = new PersonData(TbFirstname.Text, TbLastName.Text, TbWorkPhone.Text, TbHomePhone.Text); personData.InsertData(); }
It throws an exception like this:
Additional information: INSERT statement conflict with FOREIGN KEY constraint "FK_Phones_Person". The conflict occurred in the database "NoteBookTest", table "dbo.Person", column 'id'.