Exemplo n.º 1
0
        private void deleteRecord_btn_Click(object sender, EventArgs e)
        {
            try
            {
                var Id = (int)RecordList_gv.SelectedRows[0].Cells["id"].Value;

                var record = carRentalEntities.TypeOfCars.FirstOrDefault(q => q.id == Id);

                DialogResult dr = MessageBox.Show("Are You Sure You Want to Delete This Record?",
                                                  "Delete", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);

                if (dr == DialogResult.Yes)
                {
                    //Delete vehicle from table
                    carRentalEntities.TypeOfCars.Remove(record);
                    carRentalEntities.SaveChanges();

                    PopulateGrid();
                }

                //Delete vehicle from table
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
Exemplo n.º 2
0
        private void btnResetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                var password         = txtPassword.Text;
                var confirm_password = txtConfirmPassword.Text;
                var user             = db.Users.FirstOrDefault(q => q.Id == _user.Id);

                if (password != confirm_password)
                {
                    MessageBox.Show("Passwords don't match. Please try again");
                    txtPassword.Text        = "";
                    txtConfirmPassword.Text = "";
                    txtPassword.Focus();
                }
                else
                {
                    user.password = Utils.HashPassword(password);
                    db.SaveChanges();
                    MessageBox.Show("Password has been changed");
                    Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("An Error has occured. Please try again");
            }
        }
 private void btnDeleteRecord_Click(object sender, EventArgs e)
 {
     try
     {
         DialogResult dialogResult = MessageBox.Show("Are you sure to delete this vehicle?", "Delete Car", MessageBoxButtons.YesNo);
         if (dialogResult == DialogResult.Yes)
         {
             //get ID of selected Row
             var id = (int)dgvRecordList.SelectedRows[0].Cells["Id"].Value;
             //query database for record
             var record = db.CarRentalRecords.FirstOrDefault(q => q.id == id);
             //delete vehicle from table
             db.CarRentalRecords.Remove(record);
             db.SaveChanges();
             fillDataGrid();
         }
         else
         {
             //do something else
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show($"Error: {ex.Message}");
     }
 }
        private void btnDeleteCar_Click(object sender, EventArgs e)
        {
            try
            {
                //get ID of the selected Row
                var id = (int)dgvVehicleList.SelectedRows[0].Cells["Id"].Value;

                //query database for record
                var car = db.TypesOfCars.FirstOrDefault(q => q.Id == id);

                DialogResult dialogResult = MessageBox.Show("Are you sure to delete this vehicle?", "Delete",
                                                            MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                if (dialogResult == DialogResult.Yes)
                {
                    //delete vehicle from table
                    db.TypesOfCars.Remove(car);
                    db.SaveChanges();

                    dgvVehicleList.Refresh();
                    fillDataGrid();
                }
                else
                {
                    //do something else
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
Exemplo n.º 5
0
        private void btnDeleteRecord_Click(object sender, EventArgs e)
        {
            try
            {
                // get Id of selected row
                var id = (int)gvRecordList.SelectedRows[0].Cells["Id"].Value;

                //query database for record
                var record = _db.CarRentalRecords.FirstOrDefault(q => q.id == id);

                DialogResult dr = MessageBox.Show("Are You Sure You Want To Delete This Record?",
                                                  "Delete", MessageBoxButtons.YesNoCancel,
                                                  MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    //delete vehicle from table
                    _db.CarRentalRecords.Remove(record);
                    _db.SaveChanges();

                    PopulateGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
Exemplo n.º 6
0
        private void btnDeleteRecord_Click(object sender, EventArgs e)
        {
            var id     = (int)dgvRentalRecords.SelectedRows[0].Cells["id"].Value;
            var record = _db.CarRentalRecords.FirstOrDefault(q => q.id == id);

            _db.CarRentalRecords.Remove(record);
            _db.SaveChanges();
            PopulateGrid();
        }
Exemplo n.º 7
0
        private void btnResetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                var id   = (int)dgvUserList.SelectedRows[0].Cells["id"].Value;
                var user = _db.Users.FirstOrDefault(q => q.id == id);

                var hashedPassword = Utils.DefaultHashedPassword();
                user.password = hashedPassword;
                _db.SaveChanges();
                MessageBox.Show($"{user.username}'s password has been reset!");
                PopulateGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Something went wrong.");
            }
        }
Exemplo n.º 8
0
 private void btn_ResetPass_Click(object sender, EventArgs e)
 {
     try
     {
         // get id
         //We expect Int here (data type from database column). otherwise use var./ need casting
         int id = (int)dGV_UserList.SelectedRows[0].Cells["id"].Value;
         //query
         var user            = _db.Users.FirstOrDefault(q => q.id == id);
         var hashed_password = Utils.DefaultHashPassword();
         user.password = hashed_password;
         _db.SaveChanges();
         MessageBox.Show($"Password of user {user.username} have been resert");
         populateDatag();
     }
     catch (Exception)
     {
         MessageBox.Show("You Have to select a whole row in order to edit", "Error");
     }
 }
Exemplo n.º 9
0
        private void btnResetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                //get ID of selected Row
                var id = (int)dgvUsersList.SelectedRows[0].Cells["Id"].Value;

                //query database for record
                var user            = db.Users.FirstOrDefault(q => q.Id == id);
                var hashed_password = Utils.DefaultHashedPassword();
                user.password = hashed_password;
                db.SaveChanges();

                MessageBox.Show($"{user.userName}'s Password has been reset!");
                fillDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
Exemplo n.º 10
0
        private void submit_btn_Click(object sender, EventArgs e)
        {
            try
            {
                var username = username_tb.Text;
                var roleId   = (int)role_cb.SelectedValue;
                var password = Utils.DefaultHashPassword();

                var user = new User
                {
                    username = username,
                    password = password,
                    isActive = true
                };

                carRentalEntities.Users.Add(user);
                carRentalEntities.SaveChanges();


                var userid = user.id;

                var userRole = new UserRole
                {
                    roleID = roleId,
                    userID = userid,
                };

                carRentalEntities.UserRoles.Add(userRole);
                carRentalEntities.SaveChanges();

                MessageBox.Show("New User Added Successfully...");
                _manageUsers.PopulateGrid();
                Close();
            }
            catch (Exception)
            {
                MessageBox.Show("An Error Occured...");
            }
        }
Exemplo n.º 11
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //Added Validation for make and model
                if (string.IsNullOrWhiteSpace(tbMake.Text) ||
                    string.IsNullOrWhiteSpace(tbModel.Text))
                {
                    MessageBox.Show("Please ensure that you provide a make and a model");
                }
                else
                {
                    //if(isEditMode == true)
                    if (isEditMode)
                    {
                        //Edit Code here
                        var id  = int.Parse(lblId.Text);
                        var car = _db.TypesOfCars.FirstOrDefault(q => q.Id == id);
                        car.Model = tbModel.Text;
                        car.Make  = tbMake.Text;
                        car.VIN   = tbVIN.Text;
                        car.Year  = int.Parse(tbYear.Text);
                        car.LicensePlateNumber = tbLicenseNum.Text;
                    }
                    else
                    {
                        //Added validation for make and model of cars being added

                        // Add Code Here
                        var newCar = new TypesOfCar
                        {
                            LicensePlateNumber = tbLicenseNum.Text,
                            Make  = tbMake.Text,
                            Model = tbModel.Text,
                            VIN   = tbVIN.Text,
                            Year  = int.Parse(tbYear.Text)
                        };

                        _db.TypesOfCars.Add(newCar);
                    }
                    _db.SaveChanges();
                    _manageVehicleListing.PopulateGrid();
                    MessageBox.Show("Operation Completed. Refresh Grid To see Changes");
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
Exemplo n.º 12
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var username = txtUsername.Text;
                var roleId   = (int)cboRoles.SelectedValue;
                var password = Utils.DefaultHashedPassword();

                var user = new User
                {
                    userName = username,
                    password = password,
                    isActive = true
                };
                db.Users.Add(user);
                db.SaveChanges();

                var userid = user.Id;

                var userRole = new UserRole
                {
                    roleid = roleId,
                    userid = userid
                };

                db.UserRoles.Add(userRole);
                db.SaveChanges();

                MessageBox.Show("New User Added Successfully");
                _manageUsers.fillDataGrid();
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An Error Has Ocured: {ex.Message}");
            }
        }
Exemplo n.º 13
0
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                var username = tbUsername.Text;
                var roleId   = (int)cbRoles.SelectedValue;
                var password = Utils.DefaultHashedPassword();
                var user     = new User
                {
                    username = username,
                    password = password,
                    isActive = true
                };
                _db.Users.Add(user);
                _db.SaveChanges();

                var userid = user.id;

                var userRole = new UserRole
                {
                    roleid = roleId,
                    userid = userid
                };

                _db.UserRoles.Add(userRole);
                _db.SaveChanges();

                MessageBox.Show("New User Added SUccessfully");
                _manageUsers.PopulateGrid();
                Close();
            }
            catch (Exception)
            {
                MessageBox.Show("An Error Has Occured");
            }
        }
Exemplo n.º 14
0
        private void btnDeleteCar_Click(object sender, EventArgs e)
        {
            var id  = (int)dgvVehicleList.SelectedRows[0].Cells["id"].Value;
            var car = _db.TypesOfCars.FirstOrDefault(q => q.Id == id);

            DialogResult dr = MessageBox.Show("Are you sure you want to delete this record?",
                                              "Delete", MessageBoxButtons.YesNoCancel,
                                              MessageBoxIcon.Warning);

            if (dr == DialogResult.Yes)
            {
                _db.TypesOfCars.Remove(car);
                _db.SaveChanges();
            }
            PopulateGrid();
        }
Exemplo n.º 15
0
        private void save_btn_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(make_tb.Text) || string.IsNullOrWhiteSpace(model_tb.Text))
                {
                    MessageBox.Show("Please to ensure make and model is provided:");
                }
                else
                {
                    //if true
                    if (isEditMode)
                    {
                        //edit code
                        var id  = int.Parse(id_lbl.Text);
                        var car = carRentalEntities.TypeOfCars.FirstOrDefault(q => q.id == id);
                        car.Make  = make_tb.Text;
                        car.Model = model_tb.Text;
                        car.VIN   = vin_tb.Text;
                        car.Year  = int.Parse(year_tb.Text);
                        car.LicensePlateNumber = license_tb.Text;
                    }
                    else
                    {
                        //declaration and initialisation of variable
                        var newCar = new TypeOfCar
                        {
                            LicensePlateNumber = license_tb.Text,
                            Make  = make_tb.Text,
                            Model = model_tb.Text,
                            VIN   = vin_tb.Text,
                            Year  = int.Parse(year_tb.Text)
                        };

                        carRentalEntities.TypeOfCars.Add(newCar);
                    }
                    carRentalEntities.SaveChanges();
                    _manageVehicleListing.PopulateGrid();
                    MessageBox.Show("Operation Completed. Refresh Grid to see Changes");
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
Exemplo n.º 16
0
 private void btnSave_Click(object sender, EventArgs e)
 {
     try
     {
         if (string.IsNullOrWhiteSpace(txtMake.Text) || string.IsNullOrWhiteSpace(txtModel.Text))
         {
             MessageBox.Show("Please provide a make and a model");
         }
         else
         {
             if (isEditMode)
             {
                 //Edit Code here
                 var id  = int.Parse(lblID.Text);
                 var car = db.TypesOfCars.FirstOrDefault(q => q.Id == id);
                 car.Model = txtModel.Text;
                 car.Make  = txtMake.Text;
                 car.VIN   = txtVin.Text;
                 car.Year  = int.Parse(txtYear.Text);
                 car.LicensePlateNumber = txtLicense.Text;
             }
             else
             {
                 //Add Code here
                 var newCar = new TypesOfCar
                 {
                     LicensePlateNumber = txtLicense.Text,
                     Make  = txtMake.Text,
                     Model = txtModel.Text,
                     VIN   = txtVin.Text,
                     Year  = int.Parse(txtYear.Text)
                 };
                 db.TypesOfCars.Add(newCar);
             }
             db.SaveChanges();
             _manageVehicleListing.fillDataGrid();
             MessageBox.Show("Operation Completed");
             Close();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show($"Error: {ex.Message}");
     }
 }
Exemplo n.º 17
0
        private void btn_DeleteCar_Click(object sender, EventArgs e)
        {
            try
            {
                int id = (int)dGV_RecordList.SelectedRows[0].Cells["Id"].Value;

                var record = _db.CarRentalRecords.FirstOrDefault(q => q.id == id);
                //delete
                _db.CarRentalRecords.Remove(record);
                _db.SaveChanges();
                //dGV_RecordList.Refresh();
                MessageBox.Show("Delete Success");
                populateData();
            }
            catch (Exception)
            {
                MessageBox.Show("You Have to select a whole row in order to delete", "Error");
            }
        }
Exemplo n.º 18
0
        private void btn_Save_Click(object sender, EventArgs e)
        {
            string result;

            try
            {
                if (isEditMode)
                {
                    int id  = int.Parse(lbl_Id.Text);
                    var car = _db.TypesOfCars.FirstOrDefault(q => q.Id == id);
                    car.Make  = txtBox_Name.Text;
                    car.Model = txtBox_Model.Text;
                    car.VIN   = txtBox_VIN.Text;
                    car.Year  = int.Parse(txtBox_Year.Text);
                    car.LicensePlateNumber = txtBox_LicensePlate.Text;
                    result = "Edit Success";
                }
                else
                {
                    var newCar = new TypesOfCar
                    {
                        Make  = txtBox_Name.Text,
                        Model = txtBox_Model.Text,
                        VIN   = txtBox_VIN.Text,
                        Year  = int.Parse(txtBox_Year.Text),
                        LicensePlateNumber = txtBox_LicensePlate.Text
                    };
                    _db.TypesOfCars.Add(newCar);
                    result = "Add Success";
                }
                _db.SaveChanges();
                _manageVehicleListing.populateData();
                MessageBox.Show(result);
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Please check all the fields");
            }
        }
Exemplo n.º 19
0
        private void btn_DeleteCar_Click(object sender, EventArgs e)
        {
            try
            {
                int id = (int)dGV_VehicleList.SelectedRows[0].Cells["Id"].Value;

                var car = _db.TypesOfCars.FirstOrDefault(q => q.Id == id);
                //delete
                if (MessageBox.Show("Are you sure you want to delete this?", "Confirmation",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    _db.TypesOfCars.Remove(car);
                    _db.SaveChanges();
                    MessageBox.Show("Delete Success");
                }
                populateData();
            }
            catch (Exception)
            {
                MessageBox.Show("You Have to select a whole row in order to delete", "Error");
            }
        }
Exemplo n.º 20
0
        private void btnResetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                var password          = tbNewPassword.Text;
                var confirmedPassword = tbConfirmPassword.Text;
                var user = _db.Users.FirstOrDefault(q => q.id == _user.id);

                if (password != confirmedPassword)
                {
                    MessageBox.Show("Passwords dont match.Try again!");
                }
                user.password = Utils.HashPassword(password);
                _db.SaveChanges();
                MessageBox.Show("Password reset succesfully.");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has ocurrer!");
            }
        }
Exemplo n.º 21
0
        private void btnReset_Click(object sender, EventArgs e)
        {
            try
            {
                var password         = tbPassword.Text;
                var confirm_password = tbConfirmPassword.Text;
                var user             = _db.Users.FirstOrDefault(q => q.id == _user.id);
                if (password != confirm_password)
                {
                    MessageBox.Show("Password do not match. Please try again!");
                }

                user.password = Utils.HashPassword(password);
                _db.SaveChanges();
                MessageBox.Show("Password was reset successfully");
                Close();
            }
            catch (Exception)
            {
                MessageBox.Show("An Error has occured. Please try Again!");
                //throw;
            }
        }
Exemplo n.º 22
0
 private void btnSaveChanges_Click(object sender, EventArgs e)
 {
     try
     {
         if (isEditMode)
         {
             var id  = int.Parse(lblId.Text);
             var car = _db.TypesOfCars.FirstOrDefault(q => q.Id == id);
             car.Model = tbModel.Text;
             car.Make  = tbMake.Text;
             car.Year  = int.Parse(tbYear.Text);
             car.VIN   = tbVIN.Text;
             car.LicensePlateNumber = tbLPN.Text;
         }
         else
         {
             var newCar = new TypesOfCar
             {
                 LicensePlateNumber = tbLPN.Text,
                 Model = tbModel.Text,
                 Make  = tbMake.Text,
                 Year  = int.Parse(tbYear.Text),
                 VIN   = tbVIN.Text
             };
             _db.TypesOfCars.Add(newCar);
         }
         _db.SaveChanges();
         _manageVehicleListing.PopulateGrid();
         MessageBox.Show("Changes saved");
         Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo n.º 23
0
 private void btn_ResetPass_Click(object sender, EventArgs e)
 {
     try
     {
         var pass1 = txtBox_Pass1.Text;
         var pass2 = txtBox_Pass2.Text;
         var user  = _db.Users.FirstOrDefault(q => q.id == _user.id);
         if (pass1 != pass2)
         {
             MessageBox.Show("Password do not match. Please try again");
         }
         else
         {
             user.password = Utils.HashPassword(pass1);
             _db.SaveChanges();
             passwordChanged = true;
             MessageBox.Show("Reset Succssesfully");
             Close();
         }
     }catch (Exception ex)
     {
         MessageBox.Show($"Error: {ex.Message}");
     }
 }
Exemplo n.º 24
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;
            }
        }
Exemplo n.º 25
0
        private void submit_btn_Click(object sender, EventArgs e)
        {
            //Validating form
            try
            {
                string customerName = customer_tb.Text;
                var    rented       = rent_picker.Value;
                var    returned     = return_picker.Value;
                double cost         = Convert.ToDouble(cost_tb.Text);

                var car     = car_cb.Text;
                var isValid = true;
                var error   = "";

                //Checking if customer did not enter name field
                if (string.IsNullOrWhiteSpace(customerName) || string.IsNullOrWhiteSpace(car))
                {
                    isValid = false;
                    error  += "Error: Please Enter Missing Information.";
                }
                if (rented > returned)
                {
                    isValid = false;
                    error  += "Error: Illegal Date Selection";
                }
                if (isValid)
                {
                    //Object declaration of rentalrecord
                    var rentalRecord = new CarRentalRecord();

                    if (isEditMode)
                    {
                        var id     = int.Parse(recordId_lbl.Text);
                        var record = carRentalEntities.CarRentalRecords.FirstOrDefault(q => q.ID == id);
                    }

                    //Values populated from form details
                    rentalRecord.Customer_Name = customerName;
                    rentalRecord.DateRented    = rented;
                    rentalRecord.DateReturned  = returned;
                    rentalRecord.Cost          = (decimal)cost; //Casting cost which is of double data type to decimal which is type in DB
                    rentalRecord.TypeOfCarID   = (int)car_cb.SelectedValue;

                    if (!isEditMode)
                    {
                        carRentalEntities.CarRentalRecords.Add(rentalRecord);
                    }
                    carRentalEntities.SaveChanges();


                    MessageBox.Show($"Customer: {customerName} \n\r" +
                                    $"Rent Date: {rented}\n\r" +
                                    $"Return Date: {returned}\n\r" +
                                    $"Cost: ${cost}\n\r" +
                                    $"Car Type: {car}\n\r" +
                                    $"THANK YOU FOR YOUR BUSINESS");

                    Close();
                }
                else
                {
                    MessageBox.Show(error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw; // Normally ends program once exception occurs.
            }
        }
Exemplo n.º 26
0
        private void Btn_Submit_Click(object sender, EventArgs e)
        {
            string   coustomerName = txtBox_CoustomerName.Text;
            string   cartype       = cmBox_TypeOfCar.Text;
            DateTime daterented    = dateRented.Value;
            DateTime datereturned  = dateReturned.Value;
            double   cost          = toDouble(txtBox_Cost.Text);
            bool     isValid       = true;
            string   errorMessage  = "";

            if (string.IsNullOrWhiteSpace(coustomerName) || string.IsNullOrWhiteSpace(cartype))
            {
                isValid       = false;
                errorMessage += "Error: Please enter the missing data.\n\r";
            }
            if (daterented > datereturned)
            {
                isValid       = false;
                errorMessage += "Error: Illegal Date Selection\n\r";
            }
            if (isValid)
            {
                var rentalRecord = new CarRentalRecord();
                if (isEditMode)
                {
                    var id = int.Parse(lblRecordId.Text);
                    rentalRecord = _db.CarRentalRecords.FirstOrDefault(q => q.id == id);
                }
                rentalRecord.CustomerName = coustomerName;
                rentalRecord.DateRented   = daterented;
                rentalRecord.DateReturned = datereturned;
                rentalRecord.Cost         = (decimal)cost;
                rentalRecord.TypeOfCarID  = (int)cmBox_TypeOfCar.SelectedValue;
                if (rentalRecord.Cost <= 0)
                {
                    MessageBox.Show("cost cant be 0 or less.");
                }
                else
                {
                    if (!isEditMode)
                    {
                        _db.CarRentalRecords.Add(rentalRecord);
                    }

                    _db.SaveChanges();

                    if (_manageRentalRecords != null)
                    {
                        _manageRentalRecords.populateData();
                    }

                    MessageBox.Show("Thank You for renting " + coustomerName,
                                    "Thank you!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.Close();
                }
            }

            else
            {
                MessageBox.Show(errorMessage);
            }
        }
Exemplo n.º 27
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;
            }
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string customerName = txtUsername.Text;
                var    dateOut      = dtRented.Value;
                var    dateIn       = dtReturned.Value;
                var    carType      = cboCarType.Text;
                double cost         = Convert.ToDouble(txtCost.Text);
                var    isValid      = true;
                var    errorMessage = "";

                if (string.IsNullOrEmpty(customerName) || string.IsNullOrEmpty(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)
                {
                    //Declare an object of the record to be added
                    var rentalRecord = new CarRentalRecord();
                    if (isEditMode)
                    {
                        var id = int.Parse(lblRecorId.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.carID        = (int)cboCarType.SelectedValue;

                    // if not in edit mode, then add the record object to DB
                    if (!isEditMode)
                    {
                        db.CarRentalRecords.Add(rentalRecord);
                    }

                    //Save changes to entity
                    db.SaveChanges();

                    MessageBox.Show("Data Saved Successfully");
                    DialogResult dialogResult = MessageBox.Show("Want to Add new Record?", "New Record", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        txtUsername.Text = "";
                        txtUsername.Focus();
                        txtCost.Text = "";
                        var addRentalRecord = new AddEditRentalRecord();
                        addRentalRecord.Show();
                    }
                    else
                    {
                        var manageRentalRecord = new ManageRentalRecords();
                        manageRentalRecord.Show();
                        this.Close();
                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show(errorMessage);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }