locked
Problem throwing ValidationException during AssociationChanged EF event RRS feed

  • Question

  • User1034852846 posted

    I'm using a vanilla ASP.NET 3.5 SP1 Dynamic Data Entity Web Application with the following  partial class :- 

     
    public partial class Lead
        {
            public Lead()
            {
                this.SourceChannelReference.AssociationChanged += new CollectionChangeEventHandler(SourceChannel_Changed);
            }
    
            private void SourceChannel_Changed
            (
                object sender,
                CollectionChangeEventArgs e
            )
            {
                if (SourceEvent == null)
                {
                    throw new ValidationException("You must enter an Event");
                }
            }
        }

     

     And get the following result when the ValidationException is thrown:-

     

    ' ValidationException was unhandled by user code'

     

    With the following stack trace:-

    <title>You must enter an Event</title><style> body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } </style>

    Server Error in '/' Application.

    You must enter an Event

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ComponentModel.DataAnnotations.ValidationException: You must enter an Event

    Source Error:

    Line 41:             if (SourceEvent == null)
    Line 42:             {
    Line 43:                 throw new ValidationException("You must enter an Event");
    Line 44:             }
    Line 45:         }

    Source File: C:\depot\mourant-chatm\MarcinDobosz\MarcinDobosz\PipelineMetadata.cs    Line: 43

    Stack Trace:

    [ValidationException: You must enter an Event]
       MarcinDobosz.Lead.SourceChannel_Changed(Object sender, CollectionChangeEventArgs e) in C:\depot\mourant-chatm\MarcinDobosz\MarcinDobosz\PipelineMetadata.cs:43
       System.Data.Objects.DataClasses.RelatedEnd.OnAssociationChanged(CollectionChangeAction collectionChangeAction, Object entity) +67
       System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWithRelationships targetEntity, Boolean applyConstraints, Boolean addRelationshipAsUnchanged, Boolean relationshipAlreadyExists) +371
       System.Data.Objects.ObjectStateManager.AddEntityToCollectionOrReference(MergeOption mergeOption, IEntityWithRelationships sourceEntity, AssociationEndMember sourceMember, IEntityWithRelationships targetEntity, AssociationEndMember targetMember, Boolean setIsLoaded, Boolean relationshipAlreadyExists, Boolean inKeyEntryPromotion) +217
       System.Data.Objects.ObjectStateManager.UpdateRelationships(ObjectContext context, MergeOption mergeOption, AssociationSet associationSet, AssociationEndMember sourceMember, EntityKey sourceKey, IEntityWithRelationships sourceEntity, AssociationEndMember targetMember, IList targetEntities, Boolean setIsLoaded) +441
       System.Data.Common.Internal.Materialization.Shaper.FullSpanAction(IEntityWithRelationships sourceEntity, IList`1 spannedEntities, AssociationEndMember targetMember) +317
       System.Data.Common.Internal.Materialization.Shaper.HandleFullSpanElement(T_SourceEntity entity, T_TargetEntity spannedEntity, AssociationEndMember targetMember) +189
       lambda_method(ExecutionScope , Shaper ) +16408
       System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) +91
       System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +70
       System.Data.Objects.ObjectViewQueryResultData`1..ctor(IEnumerable queryResults, ObjectContext objectContext, Boolean forceReadOnlyList, EntitySet entitySet) +372
    
    [TargetInvocationException: Exception has been thrown by the target of an invocation.]
       System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType) +0
       System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType) +15
       System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +249
       System.Data.Objects.ObjectViewFactory.CreateViewForQuery(TypeUsage elementEdmTypeUsage, IEnumerable`1 queryResults, ObjectContext objectContext, Boolean forceReadOnly, EntitySet singleEntitySet) +791
       System.Data.Objects.ObjectResult`1.GetIListSourceListInternal() +109
       System.Data.Objects.ObjectResult.System.ComponentModel.IListSource.GetList() +7
       System.Data.Objects.ObjectQuery`1.GetIListSourceListInternal() +21
       System.Data.Objects.ObjectQuery.System.ComponentModel.IListSource.GetList() +7
       System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments, Creator qbConstructor) +1168
       System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +154
       System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +19
       System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
       System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
       System.Web.UI.WebControls.GridView.DataBind() +4
       System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
       System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +72
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +44
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Control.PreRenderRecursiveInternal() +171
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
    <!-- [ValidationException]: You must enter an Event at MarcinDobosz.Lead.SourceChannel_Changed(Object sender, CollectionChangeEventArgs e) in C:\depot\mourant-chatm\MarcinDobosz\MarcinDobosz\PipelineMetadata.cs:line 43 at System.Data.Objects.DataClasses.RelatedEnd.OnAssociationChanged(CollectionChangeAction collectionChangeAction, Object entity) at System.Data.Objects.DataClasses.RelatedEnd.Add(IEntityWithRelationships targetEntity, Boolean applyConstraints, Boolean addRelationshipAsUnchanged, Boolean relationshipAlreadyExists) at System.Data.Objects.ObjectStateManager.AddEntityToCollectionOrReference(MergeOption mergeOption, IEntityWithRelationships sourceEntity, AssociationEndMember sourceMember, IEntityWithRelationships targetEntity, AssociationEndMember targetMember, Boolean setIsLoaded, Boolean relationshipAlreadyExists, Boolean inKeyEntryPromotion) at System.Data.Objects.ObjectStateManager.UpdateRelationships(ObjectContext context, MergeOption mergeOption, AssociationSet associationSet, AssociationEndMember sourceMember, EntityKey sourceKey, IEntityWithRelationships sourceEntity, AssociationEndMember targetMember, IList targetEntities, Boolean setIsLoaded) at System.Data.Common.Internal.Materialization.Shaper.FullSpanAction[T_TargetEntity](IEntityWithRelationships sourceEntity, IList`1 spannedEntities, AssociationEndMember targetMember) at System.Data.Common.Internal.Materialization.Shaper.HandleFullSpanElement[T_SourceEntity,T_TargetEntity](T_SourceEntity entity, T_TargetEntity spannedEntity, AssociationEndMember targetMember) at lambda_method(ExecutionScope , Shaper ) at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() at System.Data.Objects.ObjectViewQueryResultData`1..ctor(IEnumerable queryResults, ObjectContext objectContext, Boolean forceReadOnlyList, EntitySet entitySet) [TargetInvocationException]: Exception has been thrown by the target of an invocation. at System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType) at System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Data.Objects.ObjectViewFactory.CreateViewForQuery[TElement](TypeUsage elementEdmTypeUsage, IEnumerable`1 queryResults, ObjectContext objectContext, Boolean forceReadOnly, EntitySet singleEntitySet) at System.Data.Objects.ObjectResult`1.GetIListSourceListInternal() at System.Data.Objects.ObjectResult.System.ComponentModel.IListSource.GetList() at System.Data.Objects.ObjectQuery`1.GetIListSourceListInternal() at System.Data.Objects.ObjectQuery.System.ComponentModel.IListSource.GetList() at System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect[T](DataSourceSelectArguments arguments, Creator qbConstructor) at System.Web.UI.WebControls.EntityDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) at System.Web.UI.WebControls.DataBoundControl.PerformSelect() at System.Web.UI.WebControls.BaseDataBoundControl.DataBind() at System.Web.UI.WebControls.GridView.DataBind() at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() at System.Web.UI.Control.EnsureChildControls() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) [HttpUnhandledException]: Exception of type 'System.Web.HttpUnhandledException' was thrown. at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.dynamicdata_pagetemplates_list_aspx.ProcessRequest(HttpContext context) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\0dc7a4e1\1c2655c5\App_Web_fgcoctf6.2.cs:line 0 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) --> 


     

    Can anyone help me !!!!! Please !!!!!!

     

     

    Tuesday, January 13, 2009 12:30 PM

All replies

  • User-1005219520 posted

    It looks like you are throwing an exception when there is nothing to catch it. What are you trying to do? Take a look at Customizing Validation for All Data Fields by Using a Partial-Class Method in the ADO.NET Entity Framework in http://msdn.microsoft.com/en-us/library/cc488527.aspx

    Tuesday, January 13, 2009 1:00 PM
  • User1034852846 posted

    What I'm trying to do is check the text of a foreign key edit field and then validate other fields (of the Lead object) depending on its value.

    I was expecting the ValidationException to be caught by the DynamicValidator on the Edit page.

    I have tried several ways to attempt this type of data validation but as yet have not come up with a solution !!!

     

    Tuesday, January 13, 2009 4:49 PM
  • User-1005219520 posted

    Can you pose the question with the NorthWind DB (Or AdventureWOrks)? 

    Tuesday, January 13, 2009 5:11 PM
  • User1034852846 posted

    It's not easy to fit my requirements to the Northwind DB, but here goes !

    When validating an Order, check the Customer (foreign key field) and if it is 'Around the Horn' make sure that the Order.ShipName has a value.

     

    Tuesday, January 13, 2009 5:30 PM
  • User-1005219520 posted
    Take a look at Customizing Validation for All Data Fields by Using a Partial-Class Method in the ADO.NET Entity Framework in http://msdn.microsoft.com/en-us/library/cc488527.aspx

    Then check the custID.addr field and decide if you need Shipname.

    Tuesday, January 13, 2009 6:59 PM