I am using Entity Framework and Telerik RadGrid. I have a table with a constraint which throws an exception if a product with a duplicate name is tried as an insert. I am trying to catch the exception in my business layer and it seems to run through the catch block fine but I get an error from the Telerik RadScriptManager
“Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Exception has been thrown by the target of an invocation.”
instead of the Jquery popup that I am expecting with the message “Duplicate Product” Does anyone know what I am doing wrong? does the exception need to behandled in my DAL? but I dont think i should throw BusinessRuleExceptions from somewhere besides the BLL. I have posted the Insert function in my BL class below, if anyone has an idea what might be causing the Jscript error, please let me know, thanks!!
Edit
The Object Data Source TypeName is tied to the Business layer ProductBL
Product BL function Insert_Product is being called from my ObjectDataSource as the Insert function. In the Product.cs code behind class I have a function for Inserting Products where I am passing the product Name (see below), this function has a try catch bloeck..should I be throwing the exception here? I thought it would be right to throw the BusinessRulException in the Business Layer.
Product.CS class (Object Data Source Insert command)
protected void ODSProducts_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
try
{
TextBox txtProductName = (TextBox)ProductsGrid.MasterTableView.GetInsertItem().FindControl("txtProductName");
((ACME.DAL.Product)e.InputParameters[0]).Product.product_name = txtProductName.Text;
}
catch (Exception ex)
{
HTMLError.HtmlError.LogHtmlError(ex, Application["ErrorLog"].ToString());
throw;
}
}
ProductsBL.CS
public void Insert_Product(Product product)
{
try
{
repository.Insert_Product(product);
}
catch (Exception ex)
{
if (ex.GetType().Name == "UpdateException")
{
throw new BusinessRuleException("Duplicate Product");
}
}
}
Product.DAL
public void InsertProduct(Product product)
{
context.Products.AddObject(product);
context.SaveChanges();
}
ok I figured this out after wasting too many hours on this. It seems like Telerik doesnt like me throwing the Business rule exception from the Business Layer class. So I ended up handling the exception instead in the codebehind in the object data source_Inserted event. This is what I did to display the error in a RadAlertWindow from the event.
I dont like checking the .InnerException.Message.Contains bit but thats the only way I knew how to make sure if the exception was SQL throwing duplicate error on the unique constraint on the field. If someone knows of a more elegant way to do this please share. Hope this helps someone else as well.