partial void BeforeDelete(DeleteRowContext <MyProduct, DBDataContext, MyProductsJournalDataSourceView.Row, long> context);
        private void DeleteRow(long id, MyProductsEdit.RecordEvents recordEvents, bool loadRoot)
        {
            var item = source.GetRecord(id);

            if (item != null)
            {
                var args = new RecordEventArgs();
                recordEvents.RecordDeleting(args, id);

                if (args.Cancel)
                {
                    AddErrorMessage(args.GetFullErrorMessage());
                    return;
                }

                if (!AccessOptions.CheckPermitDelete() ||
                    (item.Information != null && !item.Information.CanDelete))
                {
                    AddErrorMessage(Nat.Web.Controls.Properties.Resources.ENoPermitToDeleteRecord);
                    return;
                }

                if (!item.CanDelete)
                {
                    var messages = source.View.GetDeleteErrors(id, MyProductsResources.Header);
                    if (messages.All(string.IsNullOrEmpty))
                    {
                        AddErrorMessage(Nat.Web.Controls.Properties.Resources.ENoPermitToDeleteRecord);
                    }
                    else
                    {
                        AddErrorMessage(messages.Where(r => !string.IsNullOrEmpty(r)));
                    }
                    return;
                }


                BeforeDelete(item);
                var contextInfo = new DeleteRowContext <MyProduct, DBDataContext, MyProductsJournalDataSourceView.Row, long>
                {
                    Row       = item,
                    Key       = item.id,
                    TableItem = item.Item,
                    DB        = source.View.DB,
                };
                BeforeDelete(contextInfo);
                if (contextInfo.Cancel)
                {
                    AfterDelete(false, item);
                    AddErrorMessage(contextInfo.CancelMessage);
                    return;
                }

                try
                {
                    var delArgs = new DeleteEventArgs <long>();
                    contextInfo.DeleteEventArgs = delArgs;
                    if (contextInfo.SuccessfullRowDeleted != null && !contextInfo.SuccessfullRowDeleted.Value)
                    {
                        AfterDelete(false, item);
                        AddErrorMessage(contextInfo.FailedRowDeletedMessage);
                        return;
                    }

                    if (!delArgs.IsDeleted)
                    {
                        DeleteRow(item, delArgs);
                    }

                    if (!delArgs.IsDeleted)
                    {
                        source.View.DB.MyProducts.DeleteOnSubmit(item.Item);
                        source.View.DB.SubmitChanges();
                    }

                    contextInfo.SuccessfullRowDeleted = true;
                    AfterDelete(true, item);
                    if (delArgs.IsDeleted)
                    {
                        if (delArgs.NewSelectedValue != null)
                        {
                            ResetSelectedIndex(delArgs.NewSelectedValue.Value);
                        }
                        else
                        {
                            EmptySelectedIndex();
                        }
                    }
                }
                catch (SqlException exception)
                {
                    contextInfo.SuccessfullRowDeleted = false;
                    AfterDelete(false, item);
                    if (exception.Number != 547)
                    {
                        AddErrorMessage(exception.ToString());
                    }
                    else
                    {
                        AddErrorMessage(Nat.Web.Controls.Properties.Resources.ECanNotDeleteUseInSystem);
                    }
                }

                store.Reload();
                OnSelectedIndexChanged(EventArgs.Empty);
            }
        }
Ejemplo n.º 3
0
 partial void BeforeDelete(DeleteRowContext <DIC_MySecondDictionary, DBDataContext, DIC_MySecondDictionaryJournalDataSourceView.Row, long> context);