locked
Can DynamicValidator handle exception thrown in OnPropertyChanging methods ? RRS feed

  • Question

  • User-644412204 posted

    Hello,

    I'm using a DetailsView to create / update an entity. I've added custom validation logic in the extensibility methods for 2 DateTime properties, StartDate and EndDate. So I have an OnStartDateChanging() and an OnEndDateChanging() methods that simply throw a new ValidationException if Value <= DateTime.Now.

    I've added a DynamicValidator control to the page but it does not catch the validation exception. I noticed the actual type of the exception thrown is EntityDataSourceValidationException, could this be the reason ? How can I handle the exception in my UI then ? Is there a simpler way than implementing a new validator class derived from DynamicValidator ?

    When I implement the validation in a SaveChanges event handler instead of in the OnPropertyChanging handlers, the DynamicValidator does catch the exception correctly (though only if place ABOVE the DetailsView and UNDER the ValidationSummary control... weird)

    Anyone has any insight on this ?

    Thanks
    Yacine

    Thursday, March 4, 2010 10:56 AM

All replies

  • User-330204900 posted

    Hi Yacine, is this VS2008 SP1 or VS2010 RC? and is it a custom page or one of the standard PageTemplates? 

    Friday, March 5, 2010 9:04 AM
  • User-644412204 posted

    Hi Steve,

    Sorry for my late response. I'm using VS2010RC. The page is a custom page.

    I made some more tests on this issue. It seems that exceptions thrown in the OnPropertyChanging methods, when used with dynamic fields or controls, are caught by the DynamicValidators in the corresponding field templates, so there's no need for another DynamicValidator in the aspx page with the DetailsView. Adding a DynamicValidator in the page actually cancels the displaying of validation errors in the ValidationSummary! not sure why... So in this case, I'm simply removing the DynamicValidator from the page itself, letting the field templates handle the validation UI.

    The other case is when validation is handled in a SaveChanges event handler. In this case, if there is no DynamicValidator in the page, the exception is unhandled. But when I do add a DynamicValidator to validate the databound control (a FormView this time), then no validation message is displayed in the ValidationSummary even though the update is not performed - I checked, validation code does execute and the ValidationException does get thrown, but it is not caught by the DynamicValidator.

    I wish I could get to the bottom of this story... Does anyone know ?

    Yacine 

    Monday, March 8, 2010 9:33 AM
  • User-330204900 posted

    Yes using FormView with validation is complex you need to specify the Validation Group for the method you are using in the form view see http://csharpbits.notaclue.net/search/label/Validation%20Groups which has several examples of using validation groups. 

    Monday, March 8, 2010 9:39 AM
  • User-644412204 posted

    Follow-up :

    It turns out in the case where a ValidationException is thrown in a SaveChanges handler, the DynamicValidator attached to the FormView in the page does catch an display the exception in the ValidationSummary, but ONLY if the ValidationSummary is placed BEFORE the DynamicValidator in the page. If the ValidationSummary is placed below the DynamicValidator, nothing gets displayed in the ValidationSummary even though the validation takes place (and the Exception is thrown, as verified by stepping though code).

    Any idea of the reason for this ?

    Thanks

    Yacine

     

    Monday, March 8, 2010 9:43 AM
  • User-644412204 posted

    Update : I removed all ValidationSummary controls from my page and I have a DynamicValidator ties to my FormView (since if I don't, the ValidationException thrown in the SaveChanges handler is not handled). The result : the exception gets caught AND diplayed in the page, even though I DON'T have a ValidationSummary control...

    OK it's official : this time I'm really lost. No clue what's going on with validators et al ...

    Monday, March 8, 2010 9:54 AM
  • User-644412204 posted

    Thanks Steve for the link.

    Unfortunately that does not solve my problem as I'm not using AutoGenerateRows. Right now when I drop a DynamicValidator onto the form, without setting any ValidationGroup, exceptions do get caught and messages even get DISPLAYED correctly on the page, even though I don't even have a ValidationSummary on the page!

    So my problem is not that exceptions are not caught, rather it's the way they are caught : sometimes messages display correctly even though there's no ValidationSummary (courtesy of Field Templates?), and sometimes there is no message at all even though validation worked (no update).

    strange behavior or am I missing stuff ? time will tell (maybe)

    Monday, March 8, 2010 12:40 PM
  • User-644412204 posted

    OK moving along in my investigation, turns out the DynamicControl itself is responsible for the error message correctly being displayed in my update page, so no need for a ValidationSummary here. Thus DynamicControl does catch validation exceptions thrown in a SaveChanges handler.

    The next question is, who catches validation exceptions thrown elsewhere, such as in the OnPropertyChanging extensibility methods ? Do DynamicValidators in the field templates as I hypothesized before ? stay tuned...

    Monday, March 8, 2010 12:49 PM
  • User-330204900 posted

    AFAIK, the errors bubble up and for DD to see them they must be a ValidationException. 

    Monday, March 8, 2010 12:51 PM
  • User-644412204 posted

    Yes, all my posts are related to ValidationExceptions specifically

    Monday, March 8, 2010 3:46 PM
  • User992715993 posted

    The links below helped me with catching and displaying the ValidationException errors. 

    http://books.google.com/books?id=2wg5LCKuChcC&pg=PA612&lpg=PA612&dq=How+to+catch+EntityDataSourceValidationException&source=bl&ots=HjscfOvbKF&sig=CK8X-3pNd_78Y5S3rAlv4cZGTUY&hl=en&ei=_BzkTcD9EIHBtgeR58jeCQ&sa=X&oi=book_result&ct=result&resnum=4&ved=0CCEQ6AEwAw#v=onepage&q=How%20to%20catch%20EntityDataSourceValidationException&f=false

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.itemupdated.aspx

     

    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)

    {

    if (e.Exception != null)

    {

    EntityDataSourceValidationException ve = e.Exception as EntityDataSourceValidationException;

    if (ve == null)

    {

    lblErrorMessage.Text =

    "Data error";

    }

    else

    {

    lblErrorMessage.Text = ve.Message;

    }

    e.ExceptionHandled =

    true;

    e.KeepInEditMode =

    true;

    }

    else

    {

    FormView1.DataBind();

    }

    }

    Monday, May 30, 2011 7:05 PM