Ejemplo n.º 1
0
        /// <summary>
        /// Handles the Click event of the btnPkgSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnPkgSave_Click(object sender, EventArgs e)
        {
            if (accessMode == AccessMode.Read)
            {
                MessageBox.Show("Cannot save changes in edit mode");
                return;
            }

            if (!ValidatePkgTab())
            {
                return;
            }

            if (accessMode == AccessMode.Add)
            {
                TravelPackage newPackage = new TravelPackage()
                {
                    Name        = cmbPackageID.Text,
                    Description = txtPkgDesc.Text,
                    StartDate   = datPkgStart.Value,
                    EndDate     = datPkgEnd.Value,
                    BasePrice   = double.Parse(txtPkgBasePrice.Text),
                    Commission  = null
                };
                if (double.TryParse(txtPkgCommission.Text, out double c))
                {
                    newPackage.Commission = c;
                }
                //Add package
                newPackage = PackageDB.Insert(newPackage);

                //Refresh
                FillPackageComboBox(sender, e);
                // Maybe loop through packages to find the new ID?
                cmbPackageID.SelectedIndex = cmbPackageID.Items.Count - 1;
                lblPkgStatus.Text          = "Package created";
            }
            else if (accessMode == AccessMode.Edit)
            {
                TravelPackage newPackage = new TravelPackage(selectedPackage.ID)
                {
                    Name        = cmbPackageID.Text,
                    Description = txtPkgDesc.Text,
                    StartDate   = datPkgStart.Value,
                    EndDate     = datPkgEnd.Value,
                    BasePrice   = double.Parse(txtPkgBasePrice.Text),
                    Commission  = null
                };
                if (double.TryParse(txtPkgCommission.Text, out double c))
                {
                    newPackage.Commission = c;
                }

                //Update Package
                PackageDB.Update(selectedPackage, newPackage);

                //Refresh
                int selectedIndex = cmbPackageID.SelectedIndex;
                FillPackageComboBox(sender, e);
                cmbPackageID.SelectedIndex = selectedIndex;
                lblPkgStatus.Text          = "Package updated";
            }
        }