Dynamic Data Validation Unhandled Exception

Last post 04-11-2009 9:24 AM by sjnaughton. 10 replies.

Sort Posts:

  • Dynamic Data Validation Unhandled Exception

    04-10-2009, 6:01 AM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 9:43 AM
    • Posts 16

    When writing custom validation using the OnValidate method of a partial class an unhandled exception is thrown in the page:

    System.NullReferenceException: Object reference not set to an instance of an object.

    This seems to be linked to the fact that the table has foreign key relationships (it works fine for tables without foreign keys or if I remove the foreign key relationship from the table).

    Can anyone help on this issue?

    Trace output below:

    [NullReferenceException: Object reference not set to an instance of an object.]
       System.Data.Linq.ChangeProcessor.SendOnValidate(MetaType type, TrackedObject item, ChangeAction changeAction) +197
       System.Data.Linq.ChangeProcessor.ValidateAll(IEnumerable`1 list) +130
       System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) +76
       System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +331
       System.Data.Linq.DataContext.SubmitChanges() +19
       System.Web.UI.WebControls.LinqToSqlWrapper.SubmitChanges(DataContext dataContext) +9
       System.Web.UI.WebControls.LinqDataSourceView.InsertDataObject(Object dataContext, Object table, Object newDataObject) +83
       System.Web.UI.WebControls.LinqDataSourceView.ExecuteInsert(IDictionary values) +448
       System.Web.UI.DataSourceView.Insert(IDictionary values, DataSourceViewOperationCallback callback) +86
       System.Web.UI.WebControls.DetailsView.HandleInsert(String commandArg, Boolean causesValidation) +274
       System.Web.UI.WebControls.DetailsView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +676
       System.Web.UI.WebControls.DetailsView.OnBubbleEvent(Object source, EventArgs e) +95
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       System.Web.UI.WebControls.DetailsViewRow.OnBubbleEvent(Object source, EventArgs e) +113
       System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
       System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

  • Re: Dynamic Data Validation Unhandled Exception

    04-10-2009, 8:26 AM
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571
    • TrustedFriends-MVPs

    Can you post the OnValidate method so I can duplicate with Northwind please.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under: ,
  • Re: Dynamic Data Validation Unhandled Exception

    04-10-2009, 10:50 AM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 9:43 AM
    • Posts 16

    Steve,

    The previous message was the wrong error, I have investigated some more and I have currently identified the issue as follows:

    I have a table with a status field, The allowable values are contained in another table, and this is the foreign key for the main table status value. The Dynamic Data framework helpfully creats a dropdownlist for the field with the values from the status table, as you would expect and want.

    But if you have a validation routine for that field then you will get an javascript error on the page

    Private Sub OnEditStatusChanging(ByVal value As Integer)
    Throw New ValidationException("Status changing")
    End Sub

    if you remove the foreign key constraint then the exception is handled on the page correctly by the validator display control.

    But the larger issue is that it is not clear to me when the database is accessed and changed. I really want to compare the changed status with the original status, as some  status transitions are not allowed. How do I reference the original value to compare it with the new one?

    Thanks

     

     

  • Re: Dynamic Data Validation Unhandled Exception

    04-10-2009, 12:04 PM
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571
    • TrustedFriends-MVPs

    Hi Zorgit, are you using L2S or EF?

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Dynamic Data Validation Unhandled Exception

    04-10-2009, 12:26 PM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 9:43 AM
    • Posts 16

    Steve,

    L2S

    Basically what I want to know is, when the user clicks update to change some values, what is the best way to access the current values (i.e. the ones that were there before the user changed them).

    It is not at all obvious - are the original values stored in the page somewhere, in which case where? or do I need to read the database again to test the changed values?

  • Re: Dynamic Data Validation Unhandled Exception

    04-10-2009, 2:01 PM
    Answer
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571
    • TrustedFriends-MVPs

    OK, I think the issue here is that there is no validator in the FK Edit you should add a DynamicValidator to the FK Edit FieldTemplate that may do the trick

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Dynamic Data Validation Unhandled Exception

    04-10-2009, 2:07 PM
    Answer
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571
    • TrustedFriends-MVPs

    Yep tested it that does the trick I added in c# to a test table:

     

    [MetadataType(typeof(TestMD))]
    public partial class Test
    {
        partial void OnLongChanging(string value)
        {
            throw new ValidationException("Test Eception");
        }
    
        public partial class TestMD
        {
            public object ID { get; set; }
    
            public object Short { get; set; }
    
            public object Long { get; set; }
        }
    }
    

    And I got the exeption showing on the page Big Smile 

     

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: Dynamic Data Validation Unhandled Exception

    04-10-2009, 2:14 PM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 9:43 AM
    • Posts 16

    Steve,

    Thanks that's very helpful I will make the change to the FK_Edit template.

    And for the second part of the question - how do I see the values that were in a field BEFORE the user changed it so I can make a comparison for validation purposes?

    Thanks

     

  • Re: Dynamic Data Validation Unhandled Exception

    04-10-2009, 2:58 PM
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571
    • TrustedFriends-MVPs

    the String value has the value for the field but not the other properties :(

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
  • Re: Dynamic Data Validation Unhandled Exception

    04-11-2009, 9:14 AM
    • Member
      4 point Member
    • zorgit
    • Member since 04-10-2009, 9:43 AM
    • Posts 16

    Steve,

    The answer seems to be to create a member variable in the Partial Class, and in the fields OnFieldChanging event you can store the current value in the member variable.

    Then in OnValidate you can compare the new value (which has been written in the original member variable) to the value stored previously.

    e.g. assume a table Articles with a field EditStatus (which is a Foreign Key)

    Partial Public Class Article

    REM needs to be private or protected not public or you will get a LINC error
    Protected OldStatus As Integer

    Private Sub OnEditStatusChanging(ByVal value As Integer)
      REM EditStatus has the value BEFORE the user changes it
      OldStatus = EditStatus
    End Sub

    Private Sub OnValidate(ByVal action As System.Data.Linq.ChangeAction)
      REM now EditStatus will have the new value so we can compare to the old value
      if OldStatus=SOME_VALUE and EditStatus=SOME_OTHER_VALUE
        REM this state change is not allowed so throw an exception
        Throw New ValidationException("No can do")
      End If
    End Sub

    This means that I don't need to worry about the Foreign Key issue, as there is no problem to throw the exception in OnValidate, only in OnEditStatusChanging.

    Obviously if EditStatus is not a Foreign Key then the check can be done directly in OnEditStatusChanging by comparing the current value of the EditStatus member variable to the "value" parameter.

    Thanks

     

  • Re: Dynamic Data Validation Unhandled Exception

    04-11-2009, 9:24 AM
    • Star
      12,356 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571
    • TrustedFriends-MVPs

    That's a good solution if there are more than one properties previous value you want to check. What would be ideal is if the OnXXXUpdate partial class had OldEntity and NewEntity availble in it.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
    Filed under:
Page 1 of 1 (11 items)