示例#1
0
        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                int      rowsAffected;
                Customer customer;
                string   errorMessage;
                int      index = dataGridViewCustomer.CurrentRow.Index;

                //ADD DATABASE
                if (maskedTextBoxClientCode.Enabled == true)
                {
                    customer     = customerVM.GetDisplayCustomer();
                    rowsAffected = CustomerValidation.AddCustomer(customer);
                }
                //UPDATE DATABASE
                else
                {
                    index        = dataGridViewCustomer.CurrentRow.Index;
                    customer     = customerVM.SaveCustomer(index);
                    rowsAffected = CustomerValidation.UpdateCustomer(customer);
                }
                //Insert or Update are completed
                if (rowsAffected > 0)
                {
                    errorProvider.Clear();
                    customerVM.Customers.ResetItem(index);
                    customerVM.Customers            = CustomerRepository.GetCustomer();
                    dataGridViewCustomer.DataSource = customerVM.Customers;

                    //Set up data for labelOutput
                    string outputData = string.Format("{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}\r\n{5}\r\n{6}\r\n{7}\r\n{8}\r\n{9}"
                                                      , customer.ClientCode
                                                      , customer.CompanyName
                                                      , customer.Address1
                                                      , customer.Address2
                                                      , customer.City
                                                      , customer.Province
                                                      , customer.PostalCode
                                                      , customer.YTDSale
                                                      , customer.CreditHold
                                                      , customer.Notes);

                    //Enable All Output Detail Data
                    groupBoxDetail.Visible = true;
                    labelTitle.Visible     = true;
                    labelOutput.Text       = outputData;

                    //Display calculated properties TotalYTDSales and CreditHoldCount
                    labelTotalYTD.Text        = customerVM.Customers.TotalYTDSales().ToString();
                    labelCreditHoldCount.Text = customerVM.Customers.CreditHoldCount().ToString();
                }
                //Insert or Update are not complete
                else
                {
                    if (rowsAffected == 0)
                    {
                        errorMessage = "No DB changes were made";
                    }
                    else
                    {
                        errorMessage = CustomerValidation.ErrorMessage;
                    }
                    //MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    if (CustomerValidation.ErrorMessage != "")
                    {
                        if (maskedTextBoxClientCode.Text == string.Empty || maskedTextBoxClientCode.Text == null)
                        {
                            errorProvider.SetError(maskedTextBoxClientCode, "Enter Client Code");
                        }
                        else
                        {
                            string regExClientCode = @"^[A-Z][A-Z][A-Z][A-Z][A-Z]$";
                            if (!Regex.IsMatch(maskedTextBoxClientCode.Text, regExClientCode))
                            {
                                errorProvider.SetError(maskedTextBoxClientCode, "Invalid Client Code Format");
                            }
                            else
                            {
                                errorProvider.SetError(maskedTextBoxClientCode, string.Empty);
                            }
                        }
                        if (textBoxCompanyName.Text == string.Empty)
                        {
                            errorProvider.SetError(textBoxCompanyName, "Enter Company Name");
                        }
                        else
                        {
                            errorProvider.SetError(textBoxCompanyName, string.Empty);
                        }
                        if (textBoxAddress1.Text == String.Empty)
                        {
                            errorProvider.SetError(textBoxAddress1, "Enter Address1");
                        }
                        else
                        {
                            errorProvider.SetError(textBoxAddress1, string.Empty);
                        }
                        if (Convert.ToDecimal(textBoxYTDSale.Text) < 0.00m)
                        {
                            errorProvider.SetError(textBoxYTDSale, "YTD Sales can not be negative");
                        }
                        else
                        {
                            errorProvider.SetError(textBoxYTDSale, string.Empty);
                        }
                        if (maskedTextBoxPostalCode.Text != string.Empty && maskedTextBoxPostalCode.Text != null)
                        {
                            string regExPostalCode = @"^[A-Z]\d[A-Z] \d[A-Z]\d$";
                            if (!Regex.IsMatch(maskedTextBoxPostalCode.Text, regExPostalCode))
                            {
                                errorProvider.SetError(maskedTextBoxPostalCode, "Invalid Postal Code Format");
                            }
                            else
                            {
                                errorProvider.SetError(maskedTextBoxPostalCode, string.Empty);
                            }
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "DB Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Processing Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }