示例#1
0
        private void AddCustomer(object sender, EventArgs e)
        {
            int  result;
            bool check    = true;
            int  optional = 0;

            foreach (Control a in tcp_AddCustomer.Controls)
            {
                if (a is TextBox)
                {
                    if (a.AccessibleName != "OptionalLocation" && a.AccessibleName != "VeryOptional")
                    {
                        if (a.Text == "")
                        {
                            check = false;
                        }
                    }
                    else if (a.AccessibleName == "OptionalLocation")
                    {
                        if (a.Text != "")
                        {
                            ++optional;
                        }
                    }
                }
            }

            string nextContact = DateHandler.GetDate(dtp_customer_nextcontact);
            string lastContact = DateHandler.GetDate(dtp_customer_lastcontact);

            if (!check)
            {
                MessageBox.Show("Please make sure all the neccesary fields are filled in.");
            }
            else if (optional != 0 && optional != 4)
            {
                MessageBox.Show("All address details have to be filled in!");
            }
            else if (!int.TryParse(txtb_customer_fax.Text, out result) && !int.TryParse(txtb_customer_phonenumber_1.Text, out result) && !int.TryParse(txtb_customer_phonenumber_2.Text, out result))
            {
                MessageBox.Show("fax or phonenumber are not numbers");
            }
            else if (dtp_customer_nextcontact.Value.Date < DateTime.Now.Date)
            {
                MessageBox.Show("Date of next contact cannot be in the past.");
            }
            else if (txtb_customer_email.Text.Contains("@") == false)
            {
                MessageBox.Show("Your email is invalid");
            }
            else if (txtb_customer_phonenumber_1.Text.Length < 10)
            {
                MessageBox.Show("Your phone number is invalid");
            }
            else if (txtb_customer_phonenumber_2.Text != "" && txtb_customer_phonenumber_2.Text.Length < 10)
            {
                MessageBox.Show("Your 2nd phone number is invalid");
            }
            else if (!Regex.IsMatch(txtb_customer_zipcode_1.Text, @"^[1-9][0-9]{3}\s?[a-zA-Z]{2}$"))
            {
                MessageBox.Show("Invalid zipcode");
            }
            else if (txtb_customer_zipcode_2.Text != "" && !Regex.IsMatch(txtb_customer_zipcode_1.Text, @"^[1-9][0-9]{3}\s?[a-zA-Z]{2}$"))
            {
                MessageBox.Show("Invalid 2nd zipcode");
            }
            else
            {
                dbh.OpenConnection();

                if (dbh.AddCustomer(txtb_customer_firstname.Text, txtb_customer_lastname.Text, txtb_customer_company_name.Text, txtb_customer_email.Text, txtb_customer_fax.Text, txtb_customer_streetname_1.Text, txtb_customer_housenumber_1.Text, txtb_customer_residence_1.Text, txtb_customer_zipcode_1.Text, txtb_customer_phonenumber_1.Text, txtb_customer_streetname_2.Text, txtb_customer_housenumber_2.Text, txtb_customer_residence_2.Text, txtb_customer_zipcode_2.Text, txtb_customer_phonenumber_2.Text, lastContact, txtb_customer_last_action.Text, nextContact, txtb_customer_next_action.Text))
                {
                    MessageBox.Show("Succesfully added a customer!");
                }
                else
                {
                    MessageBox.Show("An error occcured while adding a customer.");
                }

                dbh.AddNotification(txtb_customer_firstname.Text, "Creditworthy check", "Finance", " needs to be checked on creditworthyness.");
                dbh.AddNotification(txtb_customer_firstname.Text, "Add financial details", "Finance", " needs his financial details to be added");
                dbh.CloseConnection();
            }
        }