private void ButtonSaveFaculty_Click(object sender, EventArgs e) { Target.FName = TextBoxFacultyFirstName.Text; Target.LName = TextBoxFacultyLastName.Text; // Verify the emails and numbers string result; if (!StringHelpers.FormatEMail(TextBoxFacultyPrimaryEMail.Text, out result)) { MessageBox.Show("Please ensure that the primary E-Mail is valid before saving that faculty member.", "Error"); return; } if (!StringHelpers.FormatPhoneNumber(TextBoxFacultyPrimaryNumber.Text, out result)) { MessageBox.Show("Please ensure that the primary phone number is valid before saving that faculty member.", "Error"); return; } // Verify secondaries if they're specified if (TextBoxFacultySecondaryEMail.Text.Length != 0 && !StringHelpers.FormatEMail(TextBoxFacultySecondaryEMail.Text, out result)) { MessageBox.Show("Please ensure that the secondary E-Mail is valid before saving that faculty member.", "Error"); return; } if (TextBoxFacultySecondaryNumber.Text.Length != 0 && !StringHelpers.FormatPhoneNumber(TextBoxFacultySecondaryNumber.Text, out result)) { MessageBox.Show("Please ensure that the secondary phone number is valid before saving that faculty member.", "Error"); return; } if (CheckedListBoxFacultyMajors.SelectedItems.Count == 0) { MessageBox.Show("Please specify the major that this faculty member will be instructing.", "Error"); return; } Target.Contact.PrimaryEmail = TextBoxFacultyPrimaryEMail.Text; Target.Contact.PrimaryNumber = TextBoxFacultyPrimaryNumber.Text; Target.Contact.SecondaryEmail = TextBoxFacultySecondaryEMail.Text.Length == 0 ? null : TextBoxFacultySecondaryEMail.Text; Target.Contact.SecondaryNumber = TextBoxFacultySecondaryNumber.Text.Length == 0 ? null : TextBoxFacultySecondaryNumber.Text; // Find whatever majors they are going to be teaching List<string> checkedMajors = FormMain.GetSelectedNames(CheckedListBoxFacultyMajors); foreach (string major in checkedMajors) { string majorID = (from search in Program.Database.Majors where search.Major1 == major select search).First().MajorID; FacultyMajor newMajor = new FacultyMajor() { MajorID = majorID, FacultyID = Target.FacultyID, }; Target.FacultyMajors.Add(newMajor); } Program.Database.SaveChanges(); Close(); }
Faculty CreateFaculty(string fname, string lname, string pphone, string pemail, string sphone = null, string semail = null) { Contact contact = new Contact() { PrimaryEmail = pemail, PrimaryNumber = pphone, SecondaryEmail = semail, SecondaryNumber = sphone, }; Program.Database.Contacts.Add(contact); // Find whatever majors they are going to be teaching List<string> selectedMajors = GetSelectedNames(CheckedListBoxNewFacultyMajors); var majors = from search in Program.Database.Majors where selectedMajors.Contains(search.Major1) select search; Faculty result = new Faculty() { ContactID = contact.ContactID, FName = fname, LName = lname, WorkStatus = "F", }; // Create the major relationships foreach (Major major in majors) { FacultyMajor relationship = new FacultyMajor() { FacultyID = result.FacultyID, MajorID = major.MajorID, }; Program.Database.FacultyMajors.Add(relationship); } Program.Database.Faculties.Add(result); Program.Database.SaveChanges(); return result; }