private void cmbSelectStaff_SelectedIndexChanged(object sender, EventArgs e) { if (deleteSTaff == false) { string populateFields = "SELECT * FROM Staff WHERE staff_id = @staff_id"; loadDepartmentData(); loadDriversCodes(); try { cmd.Connection = conn; cmd.CommandText = populateFields; Departments var = (Departments)cmbSelectStaff.SelectedItem; cmd.Parameters["@staff_id"].Value = var.value; conn.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { txtFirstname.Text = reader["firstname"].ToString(); txtLastname.Text = reader["lastname"].ToString(); txtID.Text = reader["id_number"].ToString(); txtCellphone.Text = reader["cellphone_number"].ToString(); txtStreetName.Text = reader["street_name"].ToString(); txtStreetNumber.Text = reader["street_number"].ToString(); txtAddressSuburb.Text = reader["street_area"].ToString(); txtAddressProvince.Text = reader["address_province"].ToString(); txtCityAddress.Text = reader["address_city"].ToString(); cmbDepartment.SelectedIndex = Convert.ToInt32(reader["department_id"]); cmbLiscence.SelectedIndex = Convert.ToInt32(reader["licence_code_id"]); } reader.Close(); conn.Close(); // LOAD THE USERS BANKING DETAILS populateFields = "SELECT StaffBankingDetails.banking_id ,StaffBankingDetails.account_type, StaffBankingDetails.bank_name, StaffBankingDetails.account_number, StaffBankingDetails.branch_code, StaffBankingDetails.branch_name FROM StaffBankingDetails, Staff WHERE Staff.staff_id = @staff_id AND Staff.banking_id = StaffBankingDetails.banking_id"; cmd.Connection = conn; cmd.CommandText = populateFields; cmd.Parameters["@staff_id"].Value = var.value; conn.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { txtBankName.Text = reader["bank_name"].ToString(); txtBranchName.Text = reader["branch_name"].ToString(); txtBranchCode.Text = reader["branch_code"].ToString(); txtAccountType.Text = reader["account_type"].ToString(); txtAccountNumber.Text = reader["account_number"].ToString(); staffUpdateBankingID = reader["banking_id"].ToString(); } } catch (Exception error) { MessageBox.Show("Error: " + error.Message); } } else { Departments staff = (Departments)cmbSelectStaff.SelectedItem; if (staff == null) { MessageBox.Show("Please Select a staff memner in order to remove them."); } else { string populateFields = "DELETE FROM Staff WHERE staff_id = @staff_id"; try { cmd.Connection = conn; cmd.CommandText = populateFields; Departments var = (Departments)cmbSelectStaff.SelectedItem; cmd.Parameters["@staff_id"].Value = var.value; conn.Open(); cmd.ExecuteNonQuery(); MessageBox.Show("Staff Member Successfully Removerd."); conn.Close(); this.Close(); } catch (Exception error) { MessageBox.Show("Error: " + error.Message); } } } }
private void button1_Click(object sender, EventArgs e) { int staffid = -1; if (userUpdateStatus == false) { string checkforDuplicates = "SELECT id_number FROM Staff WHERE id_number = @id_number"; try { cmd.Connection = conn; cmd.CommandText = checkforDuplicates; cmd.Parameters["@staff_id"].Value = txtSumID.Text; cmd.Parameters["@firstname"].Value = txtSumID.Text; cmd.Parameters["@lastname"].Value = txtSumID.Text; cmd.Parameters["@id_number"].Value = txtSumID.Text; cmd.Parameters["@cellphone_number"].Value = txtSumID.Text; cmd.Parameters["@street_number"].Value = txtSumID.Text; cmd.Parameters["@street_name"].Value = txtSumID.Text; cmd.Parameters["@street_area"].Value = txtSumID.Text; cmd.Parameters["@address_province"].Value = txtSumID.Text; cmd.Parameters["@address_city"].Value = txtSumID.Text; cmd.Parameters["@department_id"].Value = txtSumID.Text; cmd.Parameters["@licence_code_id"].Value = txtSumID.Text; cmd.Parameters["@banking_id"].Value = txtSumID.Text; cmd.Parameters["@login_id"].Value = txtSumID.Text; conn.Open(); reader = cmd.ExecuteReader(); if (!reader.HasRows) { //Continue reader.Close(); conn.Close(); addLoginDetails(); string loginID = getUserLogin(); addBankingDetails(); string bankID = getUserBank(); try { string insertDept = "INSERT INTO Staff (firstname,lastname,id_number,cellphone_number,street_number,street_name,street_area,address_province,address_city,department_id,licence_code_id,banking_id,login_id) VALUES(@firstname,@lastname,@id_number,@cellphone_number,@street_number,@street_name,@street_area,@address_province,@address_city,@department_id,@licence_code_id,@banking_id,@login_id) SELECT CAST(SCOPE_IDENTITY() AS int)"; cmd.Connection = conn; cmd.CommandText = insertDept; cmd.Parameters["@firstname"].Value = txtSumFirstname.Text; cmd.Parameters["@lastname"].Value = txtSumLastname.Text; cmd.Parameters["@id_number"].Value = txtSumID.Text; cmd.Parameters["@cellphone_number"].Value = txtSumCellphone.Text; cmd.Parameters["@street_number"].Value = txtSumStreetNum.Text; cmd.Parameters["@street_name"].Value = txtSumStreet.Text; cmd.Parameters["@street_area"].Value = txtSumSuburb.Text; cmd.Parameters["@address_province"].Value = txtSumProvince.Text; cmd.Parameters["@address_city"].Value = txtSumCity.Text; Departments dep = (Departments)cmbDepartment.SelectedItem; Departments lic = (Departments)cmbLiscence.SelectedItem; Departments bank = (Departments)cmbDepartment.SelectedItem; cmd.Parameters["@department_id"].Value = dep.value; cmd.Parameters["@licence_code_id"].Value = lic.value; cmd.Parameters["@banking_id"].Value = bankID; cmd.Parameters["@login_id"].Value = loginID; conn.Open(); staffid = (Int32)cmd.ExecuteScalar(); MessageBox.Show("Successfully Added The New Staff Member"); reader.Close(); conn.Close(); //If staff if driver, we add to the TruckDrivers if (cmbDepartment.SelectedItem.ToString() == "Logistics") { cmd.Parameters.Add("@staff_idadd", SqlDbType.Int); string addDriver = "INSERT INTO TruckDrivers (staff_id) VALUES(@staff_idadd)"; try { cmd.Connection = conn; cmd.Parameters["@staff_idadd"].Value = staffid; cmd.CommandText = addDriver; conn.Open(); cmd.ExecuteNonQuery(); } catch (Exception error) { MessageBox.Show("Error: " + error.Message); } finally { conn.Close(); } } this.Close(); } catch (Exception error) { MessageBox.Show("Error: " + error.Message); } } else { MessageBox.Show("This person is already employed by you."); conn.Close(); } reader.Close(); conn.Close(); } catch (Exception error) { MessageBox.Show("Error: " + error.Message); } } else // THIS IS THE USER STAFF DETAILS UPDATE { //Continue reader.Close(); conn.Close(); try { string insertDept = "UPDATE Staff SET firstname = @firstname, lastname = @lastname, id_number = @id_number, cellphone_number = @cellphone_number, street_number = @street_number, street_name = @street_name, street_area = @street_area, address_province = @address_province, address_city = @address_city, department_id = @department_id, licence_code_id = @licence_code_id WHERE staff_id = @staff_id"; cmd.Connection = conn; cmd.CommandText = insertDept; cmd.Parameters["@firstname"].Value = txtSumFirstname.Text; cmd.Parameters["@lastname"].Value = txtSumLastname.Text; cmd.Parameters["@id_number"].Value = txtSumID.Text; cmd.Parameters["@cellphone_number"].Value = txtSumCellphone.Text; cmd.Parameters["@street_number"].Value = txtSumStreetNum.Text; cmd.Parameters["@street_name"].Value = txtSumStreet.Text; cmd.Parameters["@street_area"].Value = txtSumSuburb.Text; cmd.Parameters["@address_province"].Value = txtSumProvince.Text; cmd.Parameters["@address_city"].Value = txtSumCity.Text; Departments dep = (Departments)cmbDepartment.SelectedItem; Departments lic = (Departments)cmbLiscence.SelectedItem; cmd.Parameters["@department_id"].Value = dep.value; cmd.Parameters["@licence_code_id"].Value = lic.value; cmd.Parameters["@banking_id"].Value = staffUpdateBankingID; conn.Open(); cmd.ExecuteNonQuery(); reader.Close(); conn.Close(); } catch (Exception error) { MessageBox.Show("Error: " + error.Message); } reader.Close(); conn.Close(); // ADDING THE BANKING DETAILS NOW try { string insertDept = "UPDATE StaffBankingDetails SET bank_name = @bank_name, account_type = @account_type, account_number = @account_number, branch_name = @branch_name, branch_code = @branch_code WHERE StaffBankingDetails.banking_id = @banking_id"; cmd.Connection = conn; cmd.CommandText = insertDept; cmd.Parameters["@bank_name"].Value = txtBankName.Text; cmd.Parameters["@account_type"].Value = txtAccountType.Text; cmd.Parameters["@account_number"].Value = txtAccountNumber.Text; cmd.Parameters["@branch_name"].Value = txtBranchName.Text; cmd.Parameters["@branch_code"].Value = txtBranchCode.Text; conn.Open(); cmd.ExecuteNonQuery(); reader.Close(); conn.Close(); } catch (Exception error) { MessageBox.Show("Error: " + error.Message); } reader.Close(); conn.Close(); MessageBox.Show("Successfully Updated Staff Member"); this.Close(); } }