Exemplo n.º 1
0
        protected void AddRental_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (int.Parse(Vacancies.Text) > int.Parse(MaxVacancy.Text))
                {
                    errormsgs.Add("Vacancies should be less than or equal to the maximum vacancy.");
                }

                if (errormsgs.Count > 0)
                {
                    LoadMessageDisplay(errormsgs, "alert alert-danger");
                }
                else
                {
                    try
                    {
                        RentalsController sysmgr = new RentalsController();
                        Rentals           item   = new Rentals();

                        item.AddressID = int.Parse(AddressID.Text);

                        if (RentalTypeList.SelectedIndex == 0)
                        {
                            item.RentalTypeID = null;
                        }
                        else
                        {
                            item.RentalTypeID = int.Parse(RentalTypeList.SelectedValue);
                        }
                        item.MonthlyRent = decimal.Parse(MonthlyRent.Text);
                        item.Vacancies   = byte.Parse(Vacancies.Text);
                        item.MaxVacancy  = byte.Parse(MaxVacancy.Text);

                        if (string.IsNullOrEmpty(DamageDeposit.Text))
                        {
                            item.DamageDeposit = null;
                        }
                        else
                        {
                            item.DamageDeposit = decimal.Parse(DamageDeposit.Text);
                        }

                        if (string.IsNullOrEmpty(AvailableDate.Text))
                        {
                            item.AvailableDate = null;
                        }
                        else
                        {
                            item.AvailableDate = DateTime.Parse(AvailableDate.Text);
                        }

                        int newRentalID = sysmgr.Rentals_Add(item);

                        RentalID.Text = newRentalID.ToString();
                        errormsgs.Add("Rental has been added");
                        LoadMessageDisplay(errormsgs, "alert alert-success");
                    }
                    catch (DbUpdateException ex)
                    {
                        UpdateException updateException = (UpdateException)ex.InnerException;
                        if (updateException.InnerException != null)
                        {
                            errormsgs.Add(updateException.InnerException.Message.ToString());
                        }
                        else
                        {
                            errormsgs.Add(updateException.Message);
                        }
                        LoadMessageDisplay(errormsgs, "alert alert-danger");
                    }
                    catch (DbEntityValidationException ex)
                    {
                        foreach (var entityValidationErrors in ex.EntityValidationErrors)
                        {
                            foreach (var validationError in entityValidationErrors.ValidationErrors)
                            {
                                errormsgs.Add(validationError.ErrorMessage);
                            }
                        }
                        LoadMessageDisplay(errormsgs, "alert alert-danger");
                    }
                    catch (Exception ex)
                    {
                        errormsgs.Add(GetInnerException(ex).ToString());
                        LoadMessageDisplay(errormsgs, "alert alert-danger");
                    }
                }
            }
        }