/// <summary>
        /// Method to add a new car with all the details.
        /// </summary>
        public void AddNewCar()
        {
            var type   = comboBoxCarType.Text;
            var typeId = context.CarTypes.Where(x => x.Type == type).Select(x => x.TypeId).Distinct();
            var make   = comboBoxCarMake.Text;
            var makeId = context.CarMakes.Where(x => x.Make == make).Select(x => x.MakeId).Distinct();
            Car newCar = new Car()
            {
                TypeId = typeId.First(),
                MakeId = makeId.First(),
                Color  = textBoxCarColor.Text,
                Model  = textBoxCarModel.Text,
                Year   = Int32.Parse(textBoxCarYear.Text),
                Price  = Decimal.Parse(textBoxCarPrice.Text),
            };

            try
            {
                context.Cars.Add(newCar);
                context.SaveChanges();
                MessageBox.Show("Car added successfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot add car to database" + ex.InnerException.InnerException.Message + "\n Make " + makeId.First());
                return;
            }
        }
        public void AddNewCar()
        {
            context = new CarRentalManagementEntities();
            var type   = comboBoxCarType.Text;
            var typeId = context.CarTypes.Where(x => x.Type == type).Select(x => x.TypeId);
            var make   = comboBoxCarType.Text;
            var makeId = context.CarMakes.Where(x => x.Make == make).Select(x => x.MakeId);
            Car newCar = new Car()
            {
                TypeId = typeId.FirstOrDefault(),
                MakeId = makeId.FirstOrDefault(),
                Color  = textBoxCarColor.Text,
                Model  = textBoxCarModel.Text,
                Year   = Int32.Parse(textBoxCarYear.Text),
                Price  = Decimal.Parse(textBoxCarPrice.Text),
            };

            try
            {
                context.Cars.Add(newCar);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot add car to database" + ex.InnerException.InnerException.Message);
                return;
            }
        }
        public void AddNewCar()
        {
            context = new CarRentalManagementEntities();
            Car newCar = new Car()
            {
                Color = textBoxCarColor.Text,
                Model = textBoxCarModel.Text,
                Year  = Int32.Parse(textBoxCarYear.Text),
                Price = Int32.Parse(textBoxCarPrice.Text),
            };

            try
            {
                context.Cars.Add(newCar);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot add car to databse" + ex.InnerException.InnerException.Message);
                return;
            }
        }
        /// <summary>
        /// Method to add new type in car types.
        /// </summary>
        public void AddNewType()
        {
            CarRentalManagementEntities entities = new CarRentalManagementEntities();
            CarType carType = new CarType()
            {
                Type = textBoxCarType.Text
            };

            try
            {
                entities.CarTypes.Add(carType);
                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot add Car Type to database" + ex.InnerException.InnerException.Message);
                return;
            }

            this.DialogResult = DialogResult.OK;
            entities.Dispose();
            Close();
        }
Exemplo n.º 5
0
        private void ButtonAddCarMake_Click(object sender, EventArgs e)
        {
            CarRentalManagementEntities entities = new CarRentalManagementEntities();
            CarMake carMake = new CarMake()
            {
                Make = textBoxAddCarMake.Text
            };

            try
            {
                entities.CarMakes.Add(carMake);
                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Cannot add Car Make to database" + ex.InnerException.InnerException.Message);
                return;
            }

            this.DialogResult = DialogResult.OK;
            entities.Dispose();
            Close();
        }