private void EmployeeEditBtn_Click_1(object sender, RoutedEventArgs e) { //Create an object called CurrentEmployee of type Employee1 Employee1 CurrentEmployee = new Employee1(); //CurrentEmployee holds the values of the selectedItem by casting the EmployeeDataGrid as type Employee1. This makes it possible to grab these values CurrentEmployee = (Employee1)EmployeeDataGrid.SelectedItem; Index = EmployeeDataGrid.SelectedIndex; //Populate the text fields with their respective data from the datagrid EmployeeFNameTxt.Text = CurrentEmployee.FirstName; EmployeeLNameTxt.Text = CurrentEmployee.LastName; DateHired.Text = CurrentEmployee.DateHired.ToString(); EmployeeWageTxt.Text = CurrentEmployee.Wage.ToString(); SupervisorCB.SelectedValue = CurrentEmployee.Supervisor; EmployeeAddressTxt.Text = CurrentEmployee.Address; EmployeeCityTxt.Text = CurrentEmployee.City; cbState.Text = CurrentEmployee.State; EmployeeZipTxt.Text = CurrentEmployee.ZIP; EmployeePhoneTxt.Text = CurrentEmployee.Phone; EmployeeEmailTxt.Text = CurrentEmployee.Email; //EmployeeID holds the EmployeeID of the record that is being updated this.EmployeeID = CurrentEmployee.EmployeeID.ToString(); EmployeeEditBtn.IsEnabled = false; EmployeeAddBtn.IsEnabled = false; }
private void EmployeeDeleteBtn_Click_1(object sender, RoutedEventArgs e) { Employee1 CurrentEmployee = new Employee1(); //CurrentEmployee holds the values of the selectedItem by casting the EmployeeDataGrid as type Employee1. This makes it possible to grab these values CurrentEmployee = (Employee1)EmployeeDataGrid.SelectedItem; MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Delete " + CurrentEmployee.FirstName + " " + CurrentEmployee.LastName + "?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo); if (messageBoxResult == MessageBoxResult.Yes) { try { //Create an object called CurrentEmployee of type Employee1 EmployeeID = CurrentEmployee.EmployeeID.ToString(); //Execute the following query to delete the record from the Employee table using the EmployeeID dbContext.Database.ExecuteSqlCommand("Delete from Employee where EmployeeID = @EmployeeID", //assign parameters values new SqlParameter("EmployeeID", this.EmployeeID)); //Execute the following query to delete the record from the person table using the person ID dbContext.Database.ExecuteSqlCommand("Delete from Person where PersonID = @PersonID", new SqlParameter("PersonID", this.EmployeeID)); String DeleteName = CurrentEmployee.FirstName + " " + CurrentEmployee.LastName; MessageBox.Show(DeleteName + " deleted from Current Employees"); //Populated the datagrid so that it no longer contains the deleted record var Employee = dbContext.Employees1.SqlQuery("select * from Employees").ToList(); EmployeeDataGrid.ItemsSource = Employee; EmployeeDataGrid.SelectedIndex = 0; EmployeeFNameTxt.Text = null; EmployeeLNameTxt.Text = null; DateHired.Text = null; EmployeeWageTxt.Text = null; SupervisorCB.SelectedIndex = -1; EmployeeAddressTxt.Text = null; EmployeeCityTxt.Text = null; cbState.SelectedIndex = -1; EmployeeZipTxt.Text = null; EmployeePhoneTxt.Text = null; EmployeeEmailTxt.Text = null; this.EmployeeID = null; EmployeeAddBtn.IsEnabled = true; EmployeeEditBtn.IsEnabled = true; } catch { MessageBox.Show("Please Assign Employees New Supervisor Before Deleting This Individual"); } } }
private void EmployeeAddBtn_Click_1(object sender, RoutedEventArgs e) { //Checks to see if user has left any fields open if (EmployeeFNameTxt.Text == null || EmployeeLNameTxt.Text == null || DateHired.Text == null || EmployeeWageTxt.Text == null || SupervisorCB.SelectedIndex == -1 || EmployeeAddressTxt.Text == null || EmployeeCityTxt.Text == null || cbState.SelectedIndex == -1 || EmployeeZipTxt.Text == null || EmployeePhoneTxt.Text == null || EmployeeEmailTxt.Text == null) { MessageBox.Show("Please Enter ALL Employee Information"); } else { //iMatch holds the value to know if the room name matches any name in current room int iMatch = 0; //the for loop goes through all the records to check if there are any matches for (int iCount = 0; iCount < EmployeeDataGrid.Items.Count - 1; iCount++) { Employee1 CurrentEmployee = new Employee1(); EmployeeDataGrid.SelectedIndex = iCount;// set the index to that of the iCount CurrentEmployee = (Employee1)EmployeeDataGrid.SelectedItem; //if the new name matches a record, one is added to iMatch if (EmployeeFNameTxt.Text.ToLowerInvariant() == CurrentEmployee.FirstName.ToLowerInvariant() && EmployeeLNameTxt.Text.ToLowerInvariant() == CurrentEmployee.LastName.ToLowerInvariant() && EmployeeAddressTxt.Text.ToLowerInvariant() == CurrentEmployee.Address.ToLowerInvariant()) { iMatch++; } } if (iMatch > 0) { //asks the user if they want to add the name even tho it already exists MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Employee already exists with this name, add anyway?", "Add Confirmation", System.Windows.MessageBoxButton.YesNo); if (messageBoxResult == MessageBoxResult.Yes) { //Execute the following query to add new record to the person table. //make variables that capture the values var supervisorName = SupervisorCB.SelectionBoxItem.ToString(); string Sup = supervisorName; //Ex: ger = Holly White var EmployeeName = Sup.Split(' '); //Ex: [1] = White [0] = Holly string supervisorLastName = EmployeeName[1].ToString(); string supervisorFirstName = EmployeeName[0].ToString(); //runs query to get SuperID int query_super_ID = (from Person in dbContext.People where Person.FirstName == supervisorFirstName && Person.LastName == supervisorLastName select Person).First <Person>().personID; //converts to int int superID = Convert.ToInt32(query_super_ID); dbContext.Database.ExecuteSqlCommand("Insert into Person(FirstName, LastName, Address, City, State, Zip, Phone, Email) Values (@FirstName, @LastName, @Address, @City, @State, @Zip, @Phone, @Email)", //assign the parameters values new SqlParameter("FirstName", EmployeeFNameTxt.Text), new SqlParameter("LastName", EmployeeLNameTxt.Text), new SqlParameter("Address", EmployeeAddressTxt.Text), new SqlParameter("City", EmployeeCityTxt.Text), new SqlParameter("State", cbState.SelectionBoxItem), new SqlParameter("Zip", EmployeeZipTxt.Text), new SqlParameter("Phone", EmployeePhoneTxt.Text), new SqlParameter("Email", EmployeeEmailTxt.Text)); try { //Execute the follwoing query to add new record to the Employee table dbContext.Database.ExecuteSqlCommand("insert into Employee values((select max(personid) from person), @DateHired, @Wage, @Supervisor)", new SqlParameter("DateHired", DateHired.SelectedDate.Value.ToShortDateString()), new SqlParameter("Wage", EmployeeWageTxt.Text), new SqlParameter("Supervisor", superID)); //Populate the datagrid with the new records var Employee = dbContext.Employees1.SqlQuery("select * from Employees").ToList(); EmployeeDataGrid.ItemsSource = Employee; //select the first record in the datagrid EmployeeDataGrid.SelectedIndex = 0; String AddName = EmployeeFNameTxt.Text + " " + EmployeeLNameTxt.Text; MessageBox.Show(AddName + " Added To Current Employees"); //Reset the values in the fields EmployeeFNameTxt.Text = null; EmployeeLNameTxt.Text = null; DateHired.Text = null; EmployeeWageTxt.Text = null; SupervisorCB.SelectedIndex = -1; EmployeeAddressTxt.Text = null; EmployeeCityTxt.Text = null; cbState.SelectedIndex = -1; EmployeeZipTxt.Text = null; EmployeePhoneTxt.Text = null; EmployeeEmailTxt.Text = null; EmployeeID = null; } catch { MessageBox.Show("Please Enter ALL Employee Information"); } } else {//resets the fields EmployeeFNameTxt.Text = null; EmployeeLNameTxt.Text = null; DateHired.Text = null; EmployeeWageTxt.Text = null; SupervisorCB.SelectedIndex = -1; EmployeeAddressTxt.Text = null; EmployeeCityTxt.Text = null; cbState.SelectedIndex = -1; EmployeeZipTxt.Text = null; EmployeePhoneTxt.Text = null; EmployeeEmailTxt.Text = null; EmployeeID = null; } } else { //Execute the following query to add new record to the person table. //make variables that capture the values var supervisorName = SupervisorCB.SelectionBoxItem.ToString(); string Sup = supervisorName; //Ex: ger = Holly White var EmployeeName = Sup.Split(' '); //Ex: [1] = White [0] = Holly string supervisorLastName = EmployeeName[1].ToString(); string supervisorFirstName = EmployeeName[0].ToString(); //runs query int query_super_ID = (from Person in dbContext.People where Person.FirstName == supervisorFirstName && Person.LastName == supervisorLastName select Person).First <Person>().personID; int superID = Convert.ToInt32(query_super_ID); dbContext.Database.ExecuteSqlCommand("Insert into Person(FirstName, LastName, Address, City, State, Zip, Phone, Email) Values (@FirstName, @LastName, @Address, @City, @State, @Zip, @Phone, @Email)", //assign the parameters values new SqlParameter("FirstName", EmployeeFNameTxt.Text), new SqlParameter("LastName", EmployeeLNameTxt.Text), new SqlParameter("Address", EmployeeAddressTxt.Text), new SqlParameter("City", EmployeeCityTxt.Text), new SqlParameter("State", cbState.SelectionBoxItem), new SqlParameter("Zip", EmployeeZipTxt.Text), new SqlParameter("Phone", EmployeePhoneTxt.Text), new SqlParameter("Email", EmployeeEmailTxt.Text)); try // try catches errors relating to supervisor { //Execute the follwoing query to add new record to the Employee table dbContext.Database.ExecuteSqlCommand("insert into Employee values((select max(personid) from person), @DateHired, @Wage, @Supervisor)", new SqlParameter("DateHired", DateHired.SelectedDate.Value.ToShortDateString()), new SqlParameter("Wage", EmployeeWageTxt.Text), new SqlParameter("Supervisor", superID)); //Populate the datagrid with the new records var Employee = dbContext.Employees1.SqlQuery("select * from Employees").ToList(); EmployeeDataGrid.ItemsSource = Employee; //select the first record in the datagrid EmployeeDataGrid.SelectedIndex = 0; String AddName = EmployeeFNameTxt.Text + " " + EmployeeLNameTxt.Text; MessageBox.Show(AddName + " Added To Current Employees"); //Reset the values in the fields EmployeeFNameTxt.Text = null; EmployeeLNameTxt.Text = null; DateHired.Text = null; EmployeeWageTxt.Text = null; SupervisorCB.SelectedIndex = -1; EmployeeAddressTxt.Text = null; EmployeeCityTxt.Text = null; cbState.SelectedIndex = -1; EmployeeZipTxt.Text = null; EmployeePhoneTxt.Text = null; EmployeeEmailTxt.Text = null; EmployeeID = null; } catch { //messagebox MessageBox.Show("Please Enter ALL Employee Information"); } } } }