예제 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string customerName = tbCustomerName.Text;
                var    dateOut      = dtRented.Value;
                var    dateIn       = dtReturned.Value;
                double cost         = Convert.ToDouble(tbCost.Text);

                var carType      = cbTypeOfCar.Text;
                var isValid      = true;
                var errorMessage = "";

                if (string.IsNullOrWhiteSpace(customerName) || string.IsNullOrWhiteSpace(carType))
                {
                    isValid       = false;
                    errorMessage += "Error: Please enter missing data.\n\r";
                }

                if (dateOut > dateIn)
                {
                    isValid       = false;
                    errorMessage += "Error: Illegal Date Selection\n\r";
                }

                //if(isValid == true)
                if (isValid)
                {
                    //Declare an object of the record to be added
                    var rentalRecord = new CarRentalRecord();
                    if (isEditMode)
                    {
                        //If in edit mode, then get the ID and retrieve the record from the database and place
                        //the result in the record object
                        var id = int.Parse(lblRecordId.Text);
                        rentalRecord = _db.CarRentalRecords.FirstOrDefault(q => q.id == id);
                    }
                    //Populate record object with values from the form
                    rentalRecord.CustomerName = customerName;
                    rentalRecord.DateRented   = dateOut;
                    rentalRecord.DateReturned = dateIn;
                    rentalRecord.Cost         = (decimal)cost;
                    rentalRecord.TypeOfCarId  = (int)cbTypeOfCar.SelectedValue;
                    //If not in edit mode, then add the record object to the database
                    if (!isEditMode)
                    {
                        _db.CarRentalRecords.Add(rentalRecord);
                    }
                    //Save Changes made to the entity
                    _db.SaveChanges();
                    _manageRentalRecords.PopulateGrid();

                    MessageBox.Show($"Customer Name: {customerName}\n\r" +
                                    $"Date Rented: {dateOut}\n\r" +
                                    $"Date Returned: {dateIn}\n\r" +
                                    $"Cost: {cost}\n\r" +
                                    $"Car Type: {carType}\n\r" +
                                    $"THANK YOU FOR YOUR BUSINESS");

                    Close();
                }
                else
                {
                    MessageBox.Show(errorMessage);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw;
            }
        }
예제 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string customerName = tbCustomerName.Text;
                var    dateRented   = dtpRented.Value;
                var    dateReturned = dtpReturned.Value;
                var    carType      = cmbCarType.Text;
                var    isValid      = true;
                var    errorMessage = "";

                double cost = Convert.ToDouble(tbCost.Text);
                if (string.IsNullOrWhiteSpace(customerName) || string.IsNullOrWhiteSpace(carType))
                {
                    isValid       = false;
                    errorMessage += "Enter missing data!\n";
                }
                if (dateRented > dateReturned)
                {
                    isValid       = false;
                    errorMessage += "Ilegal date selection!\n";
                }
                if (isValid == true)
                {
                    if (isEditMode)
                    {
                        var id = int.Parse(lblRecordId.Text);
                        var carRentalRecord = _db.CarRentalRecords.FirstOrDefault(q => q.id == id);
                        carRentalRecord.CustomerName = customerName;
                        carRentalRecord.DateRented   = dateRented;
                        carRentalRecord.DateReturned = dateReturned;
                        carRentalRecord.Cost         = (decimal)cost;
                        carRentalRecord.TypeofCarId  = (int)cmbCarType.SelectedValue;
                        _db.SaveChanges();
                        MessageBox.Show($"Thank you for submitting {tbCustomerName.Text} the car {cmbCarType.Text} on date {dtpRented.Value}"
                                        + $" Cost is: {cost}");
                    }
                    else
                    {
                        var carRentalRecord = new CarRentalRecord();
                        carRentalRecord.CustomerName = customerName;
                        carRentalRecord.DateRented   = dateRented;
                        carRentalRecord.DateReturned = dateReturned;
                        carRentalRecord.Cost         = (decimal)cost;
                        carRentalRecord.TypeofCarId  = (int)cmbCarType.SelectedValue;
                        _db.CarRentalRecords.Add(carRentalRecord);
                        _db.SaveChanges();
                        MessageBox.Show($"Thank you for submitting {tbCustomerName.Text} the car {cmbCarType.Text} on date {dtpRented.Value}"
                                        + $" Cost is: {cost}");
                    }
                    _manageRentalRecords.PopulateGrid();
                    Close();
                }
                else
                {
                    MessageBox.Show(errorMessage);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //  throw;
            }
        }