private void btnConfirm_Click(object sender, EventArgs e) { bool convertContact = IsDigitsOnly(txtEmpContact.Text); bool convertPostCode = IsDigitsOnly(txtEmpPostcode.Text); bool convertName = IsCharOnly(txtEmpName.Text); double salary = 0.0; bool convertSalary = double.TryParse(txtEmpSalary.Text, out salary); if (convertContact == false) { MessageBox.Show("Please enter only numbers (no special characters) in the Contact field!", "Error"); return; } else if (convertSalary == false || salary < 0) { MessageBox.Show("Please enter positive numbers in the Salary field!", "Error"); return; } else if (convertPostCode == false || txtEmpPostcode.Text.Length != 5) { MessageBox.Show("Please enter 5 positive numbers in the Postcode field!", "Error"); return; } else if (String.IsNullOrWhiteSpace(txtEmpName.Text)) { MessageBox.Show("Please enter data in the Name field!", "Error"); return; } else if (convertName == false) { MessageBox.Show("Please enter characters in the Name field!", "Error"); return; } salary = Math.Round(salary, 2); string name = txtEmpName.Text; string contact = txtEmpContact.Text; string address = txtEmpAddress.Text; string postcode = txtEmpPostcode.Text; string city = txtEmpCity.Text; string state = cbEmpState.GetItemText(cbEmpState.SelectedItem); string position = txtPosition.Text; OleDbCommand cmd = new OleDbCommand(); OleDbConnection cn = new OleDbConnection(); cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Restaurant.accdb; Persist Security Info=False;"; cmd.Connection = cn; bool successful = false; try { //If loaded then UPDATE, if not loaded then INSERT if (loaded == true) { string q = "UPDATE Employee SET empName='" + name + "', empContact='" + contact + "', empSalary=" + salary + ", empAddress='" + address + "', empPostcode='" + postcode + "', empCity='" + city + "', empState='" + state + "', empPosition='" + position + "' WHERE empID = " + empID + ";"; cmd.CommandText = q; cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); MessageBox.Show("Record successfully edited! Returning to previous screen!", "Successful"); successful = true; } else { string p = "SELECT Max(empID) FROM Employee"; cmd.CommandText = p; cn.Open(); int maxId = 1; if (cmd.ExecuteScalar() != DBNull.Value) { maxId = Convert.ToInt32(cmd.ExecuteScalar()); maxId = maxId + 1; } cn.Close(); string q = "INSERT INTO Employee(empID, empName, empContact, empSalary, empAddress, empPostcode, empCity, empState, empPosition)" + " VALUES(" + maxId + ",'" + name + "','" + contact + "'," + salary + ",'" + address + "','" + postcode + "','" + city + "','" + state + "','" + position + "');"; cmd.CommandText = q; cn.Open(); cmd.ExecuteNonQuery(); cn.Close(); MessageBox.Show("Record successfully added! Returning to previous screen!", "Successful"); successful = true; } } catch (Exception ex) { cn.Close(); MessageBox.Show(ex.ToString()); MessageBox.Show("Oops... Something went wrong. Please contact the admin to make sure everything's all right!", "Sorry"); } if (successful == true) { frmEmployee newForm = new frmEmployee(); newForm.Show(); this.Hide(); } }