示例#1
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            String activity = "A";
            String dob      = String.Format("{0:dd-MMM-yy}", dtpDOB.Value);
            var    today    = DateTime.Today;
            var    birth    = dtpDOB.Value.Year;
            int    age      = today.Year - birth;

            if (age < 18)
            {
                MessageBox.Show("Tenant must be 18 or over!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                dtpDOB.Focus();
                return;
            }

            //Validate Data
            if (txtSurname.Text.Equals("") || txtForename.Text.Equals("") || txtPhone.Text.Equals("") || txtEmail.Text.Equals("") || dtpDOB.Equals(""))
            {
                MessageBox.Show("All fields must be entered", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            String phone = txtPhone.Text;

            foreach (char c in phone)
            {
                if (c < '0' || c > '9')
                {
                    MessageBox.Show("Phone must be numeric!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPhone.Text = "";
                    return;
                }
            }

            //Instantiate Tenant Object
            Tenant myTenant = new Tenant();

            myTenant.setTenantId(Convert.ToInt32(txtTenantID.Text));
            if (PropertySysv2.Owner.validText(txtForename.Text))
            {
                myTenant.setForename(txtForename.Text);
            }
            else
            {
                MessageBox.Show("Forename must be letters only", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtForename.Text = "";
                txtForename.Focus();
                return;
            }


            if (PropertySysv2.Owner.validText(txtSurname.Text))
            {
                myTenant.setSurname(txtSurname.Text);
            }
            else
            {
                MessageBox.Show("Address Line 2 must be letters only", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSurname.Text = "";
                txtSurname.Focus();
                return;
            }


            if (PropertySysv2.Owner.validNumbers(txtPhone.Text))
            {
                myTenant.setPhone(txtPhone.Text);
            }
            else
            {
                MessageBox.Show("Phone must be numbers only", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPhone.Text = "";
                txtPhone.Focus();
                return;
            }


            if (PropertySysv2.Owner.validEmail(txtEmail.Text))
            {
                myTenant.setEmail(txtEmail.Text);
            }
            else
            {
                MessageBox.Show("Email must be correct format!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.Focus();
                return;
            }

            myTenant.setDob(dob);
            myTenant.setActivity(activity);
            myTenant.setPropID(Convert.ToInt32(txtPropID.Text));


            //INSERT Tenant Record Into Tenant Table
            myTenant.regTenant();

            //Reset UI
            txtPropID.Enabled   = false;
            txtSurname.Enabled  = false;
            txtForename.Enabled = false;
            txtPhone.Enabled    = false;
            txtEmail.Enabled    = false;
            dtpDOB.Enabled      = false;
            lblMsg.Visible      = true;

            grpDates.Visible = true;
        }