Search code examples
asp.netlinqlinq-to-sqlgridviewlinqdatasource

Error message on using LinqDataSource in webforms


Coding Platform: ASP.NET 4.0

I am binding a GridView with LinqDataSource with AutoDelete functionality enabled.
GridView is bound to the Products Table.
I have a Products Table and a Category Table with an association on CategoryID.
If I try to delete a Category that is referred in the Products Table I cannot do that.
Its is totally acceptable, but I want the end user to be notified with some error message.
Where to catch this error message?


Solution

  • Ended up using OnDeleting event of the LinqDataSource

        protected void LinqDataSource1_Deleting(object sender, LinqDataSourceDeleteEventArgs e)
        {
            try
            {
                Categories category = (Categories)e.OriginalObject;
                if (helper.IsCategoryPresentInProductsTable(category.CategoryID))
                {
                    e.Cancel = true;
                    StatusLabel.Text = String.Format("{0} is referred in the products table. Delete aborted!",
                        category.CategoryName);
                    StatusLabel.Visible = true;
                }
            }
            catch (Exception err)
            {
                Elmah.ErrorSignal.FromCurrentContext().Raise(err);
            }
        }