Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //w taki sposób mozemy pobrać dane z combobox połączonego z bazą danych
            var display = cmb_CarType.Text;
            var value   = cmb_CarType.SelectedValue;


            //pobieranie danych z pól tekstowych i wprowadzenie ich do bazy danych
            //za pośrednictwem nowo stworzonego obiektu klasy CarRetnalRecord (automatyczna klasa stworzona na wzór tabeli w bazie danych)


            if (DataValidation())
            {
                //stworzenie nowego obiektu na cele utworzenia nowego wpisu do bazy danych
                var newr = new CarRentalRecord();

                newr.CustomerName = txt_CustomerName.Text;
                newr.DateRented   = dtp_RentDate.Value;
                newr.DateReturned = dtp_ReturnDate.Value;
                newr.Cost         = Decimal.Parse(txt_costs.Text);
                newr.CarId        = Convert.ToInt32(cmb_CarType.SelectedValue);

                carRentalEntities.CarRentalRecords.Add(newr);
                carRentalEntities.SaveChanges();

                OnAddedData(EventArgs.Empty);
                carRentalEntities.SaveChanges();
                MessageBox.Show("Data updated successfully");
                Close();
            }
            else
            {
                MessageBox.Show("Your Data is invalid\nCheck your form for incorrect data.", "Data Validation System");
            }
        }
Example #2
0
 private void PopulateFields(CarRentalRecord recordToEdit)
 {
     tbCustomerName.Text = recordToEdit.CustomerName;
     dtRented.Value      = (DateTime)recordToEdit.DateRented;
     dtReturned.Value    = (DateTime)recordToEdit.DateReturned;
     tbCost.Text         = recordToEdit.Cost.ToString();
     lblRecordId.Text    = recordToEdit.id.ToString();
 }
 private void PopulateFields(CarRentalRecord recordToEdit)
 {
     txtUsername.Text = recordToEdit.customerName;
     dtRented.Value   = (DateTime)recordToEdit.dateRented;
     dtReturned.Value = (DateTime)recordToEdit.dateReturned;
     txtCost.Text     = recordToEdit.cost.ToString();
     lblRecorId.Text  = recordToEdit.id.ToString();
 }
Example #4
0
 public AddRentalRecord(CarRentalRecord record)
 {
     InitializeComponent();
     carRentalEntities = new CarRentalEntities();
     bs          = new BindingSource();
     isEditMode  = true;
     this.record = record;
 }
 private void PopulateFileds(CarRentalRecord recordToEdit)
 {
     lblId.Text           = recordToEdit.RentalId.ToString();
     txtName.Text         = recordToEdit.Name;
     dpDateRented.Value   = (DateTime)recordToEdit.RentalDate;
     dpDateReturned.Value = (DateTime)recordToEdit.ReturnDate;
     txtCost.Text         = recordToEdit.Cost.ToString();
     //cboCarType.Text = recordToEdit.CarType.Name;
 }
Example #6
0
 private void PopulateFields(CarRentalRecord recordToEdit)
 {
     txtBox_CoustomerName.Text = recordToEdit.CustomerName;
     //cmBox_TypeOfCar.Text=recordToEdit.TypeOfCarID
     dateRented.Value   = (DateTime)recordToEdit.DateRented;
     dateReturned.Value = (DateTime)recordToEdit.DateReturned;
     txtBox_Cost.Text   = recordToEdit.Cost.ToString();
     lblRecordId.Text   = recordToEdit.id.ToString();
 }
Example #7
0
 public AddEditRentalRecord(CarRentalRecord recordToEdit, ManageRentalRecords manageRentalRecords = null)
 {
     InitializeComponent();
     lblTitle.Text = "Edit Rental Record";
     PopulateFields(recordToEdit);
     _manageRentalRecords = manageRentalRecords;
     _db        = new CarRentalEntities();
     isEditMode = true;
 }
        private void populateFields(CarRentalRecord recordToEdit)
        {
            //throw new NotImplementedException();

            customer_tb.Text    = recordToEdit.Customer_Name;
            rent_picker.Value   = (DateTime)recordToEdit.DateRented;
            return_picker.Value = (DateTime)recordToEdit.DateReturned;
            cost_tb.Text        = recordToEdit.Cost.ToString();
            recordId_lbl.Text   = recordToEdit.ID.ToString();
        }
 public AddEditRentalRecord(CarRentalRecord recordToEdit)
 {
     InitializeComponent();
     title_lbl.Text = "Edit New Rental Record";
     this.Text      = "Edit Rental Record";
     if (recordToEdit == null)
     {
         MessageBox.Show("Please to ensure that you selected a valid record to edit");
     }
     else
     {
         isEditMode        = true;
         carRentalEntities = new CarRentalEntities();
         populateFields(recordToEdit);
     }
 }
 public frmAddEditRentalRecord(CarRentalRecord recordToEdit)
 {
     InitializeComponent();
     if (recordToEdit == null)
     {
         MessageBox.Show("Please ensure that you selected a valid record to edit.");
         isValid = false;
         Close();
     }
     else
     {
         isEditMode = true;
         db         = new CarRentalEntity();
         PopulateFileds(recordToEdit);
     }
 }
Example #11
0
 public AddEditRentalRecord(CarRentalRecord recordToEdit, ManageRentalRecords manageRentalRecords)
 {
     InitializeComponent();
     label1.Text          = "Edit Vehicle";
     _manageRentalRecords = manageRentalRecords;
     if (recordToEdit == null)
     {
         MessageBox.Show("Please ensure that you selected a valid record");
         Close();
     }
     else
     {
         isEditMode = true;
         _db        = new CarRentalEntities();
         PopulateFields(recordToEdit);
     }
 }
 public AddEditRentalRecord(CarRentalRecord recordToEdit)
 {
     InitializeComponent();
     lblTitle.Text = "Edit Rental Record";
     this.Text     = "Edit Rental Record";
     if (recordToEdit == null)
     {
         MessageBox.Show("Select a record to Edit.");
         Close();
     }
     else
     {
         isEditMode = true;
         db         = new CarRentalEntities();
         PopulateFields(recordToEdit);
     }
 }
Example #13
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);
            }
        }
Example #15
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;
            }
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string   name    = txtName.Text;
            DateTime dateOut = dpDateRented.Value;
            DateTime dateIn  = dpDateReturned.Value;
            double   cost    = Convert.ToDouble(txtCost.Text);
            var      carType = cboCarType.Text;
            //bool isValid = true;
            string errorMessage = string.Empty;

            isValid = true;

            if (isValid)
            {
                var rentalRecord = new CarRentalRecord();

                if (isEditMode)
                {
                    var id = int.Parse(lblId.Text);
                    rentalRecord = db.CarRentalRecords.FirstOrDefault(q => q.RentalId == id);
                }

                rentalRecord.Name        = name;
                rentalRecord.RentalDate  = dateOut;
                rentalRecord.ReturnDate  = dateIn;
                rentalRecord.Cost        = (decimal)cost;
                rentalRecord.TypeOfCarId = (int)cboCarType.SelectedValue;

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

                db.SaveChanges();
                MessageBox.Show("Record updated successfully.");
                Close();
            }
            else
            {
                MessageBox.Show(errorMessage);
            }

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

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

                //if (isValid == true)
                //{

                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
Example #17
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);
            }
        }
        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.
            }
        }