Beispiel #1
0
        // validate the users entries
        private void btnSaveMod_Click(object sender, EventArgs e)
        {
            if (Validator.IsProvided(txtPkgName, "Package Name") &&
                Validator.IsProvided(txtPkgDesc, "Description") &&
                Validator.IsNonNegativeDouble(txtPkgBasePrice, "Base Price") &&
                Validator.IsNonNegativeDouble(txtPkgAgencyCommission, "Commission") &&
                Validator.IsCorrectLength(txtPkgName, 50))
            {
                if (Convert.ToDecimal(txtPkgBasePrice.Text) <= Convert.ToDecimal(txtPkgAgencyCommission.Text))
                {
                    MessageBox.Show("Base Price must be greater then commission", "Entry error");
                    txtPkgBasePrice.Focus();
                }
                else if (pkgEndDateDateTimePicker.Value <= pkgStartDateDateTimePicker.Value)
                {
                    MessageBox.Show(" Package End Date Must be greater then Start Date", "Entry error");
                    pkgStartDateDateTimePicker.Focus();
                }
                else
                {
                    try
                    {
                        using (TravelExpertDataContext dbContext = new TravelExpertDataContext())
                        {
                            // get the product with Code from the current text box
                            Package package = dbContext.Packages.Single(p => p.PackageId == Convert.ToInt32(txtPackageId.Text));

                            // make changes by copying values from text boxes
                            package.PkgName             = Convert.ToString(txtPkgName.Text);
                            package.PkgStartDate        = Convert.ToDateTime(pkgStartDateDateTimePicker.Value);
                            package.PkgEndDate          = Convert.ToDateTime(pkgEndDateDateTimePicker.Value);
                            package.PkgDesc             = Convert.ToString(txtPkgDesc.Text);
                            package.PkgBasePrice        = Convert.ToDecimal(txtPkgBasePrice.Text);
                            package.PkgAgencyCommission = Convert.ToDecimal(txtPkgAgencyCommission.Text);

                            dbContext.SubmitChanges();
                            DialogResult = DialogResult.OK;
                            MessageBox.Show("Package changed successfully!", "Package Modified");
                        }
                    }
                    catch (ChangeConflictException)
                    {
                        MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception");
                        DialogResult = DialogResult.Retry;
                    }
                }
Beispiel #2
0
        private void btnAddPackage_Click(object sender, EventArgs e)
        {
            if (Validator.IsProvided(txtName, "Package Name") &&
                Validator.IsProvided(txtDescription, "Description") &&
                Validator.IsNonNegativeDouble(txtPrice, "Base Price") &&
                Validator.IsNonNegativeDouble(txtCommision, "Commission") &&
                Validator.IsCorrectLength(txtName, 50))
            {
                if (Convert.ToDecimal(txtPrice.Text) <= Convert.ToDecimal(txtCommision.Text))
                {
                    MessageBox.Show("Base Price must be greater then commision", "Entry error");
                    txtPrice.Focus();
                }
                else if (pkgEndDateDateTimePicker.Value <= pkgStartDateDateTimePicker.Value)
                {
                    MessageBox.Show(" Package End Date Must be greater then Start Date", "Entry error");
                    pkgStartDateDateTimePicker.Focus();
                }
                else

                if (isAdd) // it is add
                {
                    Package newPackage = new Package
                    {
                        PkgName             = txtName.Text.ToString(),
                        PkgStartDate        = Convert.ToDateTime(pkgStartDateDateTimePicker.Value),
                        PkgEndDate          = Convert.ToDateTime(pkgEndDateDateTimePicker.Value),
                        PkgDesc             = txtDescription.Text.ToString(),
                        PkgBasePrice        = Convert.ToDecimal(txtPrice.Text),
                        PkgAgencyCommission = Convert.ToDecimal(txtCommision.Text)
                    };
                    //save to database
                    using (TravelExpertDataContext dbContext = new TravelExpertDataContext())
                    {
                        dbContext.Packages.InsertOnSubmit(newPackage);
                        dbContext.SubmitChanges(); //submit to database
                    }
                    DialogResult = DialogResult.OK;
                }
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (isAdd) // it is add
            {
                if (Validator.IsProvided(txtSupplierId, "Supplier Id") &&
                    Validator.IsInt32(txtSupplierId) &&
                    Validator.IsProvided(txtSupName, "Supplier Name") && Validator.isNotNumeric(txtSupName, "Supplier Name") &&
                    Validator.IsCorrectLength(txtSupName, 50))
                {
                    // create a supplier by taking data from form inputs
                    Supplier newSupplier = new Supplier
                    {
                        SupplierId = Convert.ToInt32(txtSupplierId.Text),
                        SupName    = txtSupName.Text
                    };
                    //save to database
                    using (TravelExpertDataContext dbContext = new TravelExpertDataContext())
                    {
                        dbContext.Suppliers.InsertOnSubmit(newSupplier);
                        dbContext.SubmitChanges(); //submit to database
                        MessageBox.Show("Changes have been saved", "Data update");
                    }

                    DialogResult = DialogResult.OK;
                }
                else // validation  failed
                {
                    DialogResult = DialogResult.Cancel;
                }
            }
            else // it is modify
            {
                if (Validator.IsProvided(txtSupName, "Supplier Name") && Validator.isNotNumeric(txtSupName, "Supplier Name") &&
                    Validator.IsCorrectLength(txtSupName, 50))
                {
                    try
                    {
                        using (TravelExpertDataContext dbContext = new TravelExpertDataContext())
                        {
                            Supplier supplierFromDb = (from sup in dbContext.Suppliers
                                                       where sup.SupplierId == currentSupplier.SupplierId
                                                       select sup).Single();

                            if (supplierFromDb != null)
                            {
                                supplierFromDb.SupName = txtSupName.Text;
                                //submit the changes to db
                                dbContext.SubmitChanges();
                                DialogResult = DialogResult.OK;
                                MessageBox.Show("Changes have been modified", "Data update");
                            }
                        }
                    }
                    catch (ChangeConflictException)
                    {
                        MessageBox.Show("Another user changed or deleted the current record", "Concurrency Exception");
                        DialogResult = DialogResult.Retry;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, ex.GetType().ToString());
                    }
                }
                else // validation failed
                {
                    DialogResult = DialogResult.Cancel;
                }
            }
        }