views:

434

answers:

3

I have 5 different entities for which dynamic data(with LINQTOSQL) was generated. On Insert(Insert.aspx) of any of these entities, if there is an error, I would like to notify user that error happened and possibly show some generic error message.

1) I am not talking about regular required field errors but something like "Unique constraint violation"

2) I can do it for each page separately by doing something like this:

protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e) {
    if (e.Exception == null || e.ExceptionHandled)
    {
        Response.Redirect(table.ListActionPath);
    }
    else
    {
        //OtherErrors is the label on the page
        OtherErrors.Visible = true;
        OtherErrors.Text = e.Exception.Message;
        OtherErrors.DataBind();
        e.ExceptionHandled = true;
        e.KeepInInsertMode = true;

    }
}

3) BUT, I want to created something very generic that will work for all inserts across all entities

+1  A: 

You can customize validation by creating an event handler in the ADO.NET Entity Framework class:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;
using System;
using System.Data;
using System.Data.Objects;

namespace AdventureWorksLTModel 
{
    public partial class AdventureWorksLTEntities 
    {
        partial void OnContextCreated() 
        {
            this.SavingChanges += new System.EventHandler(OnSavingChanges);
        }

        public void OnSavingChanges(object sender, System.EventArgs e) 
        {
            var stateManager = ((AdventureWorksLTEntities)sender).ObjectStateManager;
            var changedEntities = ObjectStateManager.GetObjectStateEntries (EntityState.Modified | EntityState.Added);

            // validation check logic
            throw new ValidationException("Something went wrong.");
        }

    }
}

Any validation exceptions that are thrown in the data model are caught by the DynamicValidator control. The page templates included with a Dynamic Data project contain a DynamicValidator control, which displays the validations errors on the page.

David Elizondo
I am specifically talking about Dynamic Data with LINQTOSQL.Even thought the solution above is great, I don't think there is anything similar for LINGTOSQL generated classes
Chicago
A: 

I can't fully test this for your situation but, you could override the SubmitChanges method.

 public partial class MyNorthwindDataContext : NorthwindDataContext
 {

  public MyNorthwindDataContext()
  {

  }

  public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
  {
   //catch error logic here...

   base.SubmitChanges(failureMode);
  }
 }
Aaron Hoffman
This works BUT it what if I do not have access to the actual exception. I can only it is in failure mode or not. Please help!!!
Chicago
I'm not sure I understand. From the SubmitChanges method, you would interrogate the 'Table<T>' collections in your DataContext and find out what changes have been made and are about to be sent to the DataStore.
Aaron Hoffman
Sorry, my bad. I figured it out. I just put try/catch around base.SubmitChanges(failureMode) and threw ValidationException. Before I was checking for ConflisMode type, that was wrong.
Chicago
A: 
    public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{

    try
    {
        base.SubmitChanges(failureMode);
    }
    catch (Exception e)
    {
        throw new ValidationException("Something is wrong", e);
    }

}
Chicago