コード例 #1
0
        protected void DeleteRental_Click(object sender, EventArgs e)
        {
            int rentalid = 0;

            if (string.IsNullOrEmpty(RentalID.Text))
            {
                errormsgs.Add("Search for a rental to Remove");
            }
            else if (!int.TryParse(RentalID.Text, out rentalid))
            {
                errormsgs.Add("Rental id is invalid");
            }
            else if (rentalid < 1)
            {
                errormsgs.Add("Rental id is invalid");
            }

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

                    //issue the BLL call
                    int rowsaffected = sysmgr.Rentals_Delete(rentalid);
                    //give feedback
                    if (rowsaffected > 0)
                    {
                        errormsgs.Add("Rental has been removed.");
                        LoadMessageDisplay(errormsgs, "alert alert-success");
                        BindRentalsList();   //by default, list will be at index 0
                        Clear_Click(sender, e);
                    }
                    else
                    {
                        errormsgs.Add("Rental has not been removed. Rental was not found.");
                        LoadMessageDisplay(errormsgs, "alert alert-danger");
                        BindRentalsList();
                        Clear_Click(sender, e);
                    }
                }
                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");
                }
            }
        }