Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Hardik Mehta 12 posts 83 karma points
    Jun 26, 2018 @ 12:17
    Hardik Mehta
    0

    CancelOperation throws exception from Custom event handler

    I am using Umbraco 7.8.1

    I have added custom event handler to prevent saving data to validate future dates in date picker

    ContentService.Saving += delegate (IContentService sender, SaveEventArgs<IContent> args)
    {
        foreach (var content in args.SavedEntities.Where(c => c.ContentType.Alias.Equals("profile")))
        {
            var birthDate = Convert.ToDateTime(content.Properties["birthDate"].Value);
    
            if (DateTime.Compare(birthDate ,DateTime.Now) > 0)
            {
                args.CancelOperation(new EventMessage("Invalid Date of Birth", "You can not add futuer date...", EventMessageType.Error));
            }
        }
    }
    

    It shows error message but it also throws an exception

    Umbraco.Web.Editors.ContentController - Unhandled controller exception occurred System.Web.Http.HttpResponseException: Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.

    and then page redirect to url : http://domain.in/umbraco/#/content/content/edit/0

    Please help me to fix this problem.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Jun 27, 2018 @ 14:40
    Dan Diplo
    0

    Not saying this will work, but there is an alternate way to do this...

    First, you need to check whether the event supports cancelling and then you can perform the same logic using:

    if (DateTime.Compare(birthDate, DateTime.Now) > 0)
    {
        if (args.CanCancel)
        {
            args.Cancel = true;
            args.Messages.Add(new EventMessage("Invalid Date of Birth", "You can not add futuer date...", EventMessageType.Error));
        }
    }
    

    As I say, it may not help, but worth a try.

    The other thing to do is create a breakpoint in VS and step through to see what might be happening.

Please Sign in or register to post replies

Write your reply to:

Draft