Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4034388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:57:57+00:00 2026-05-20T11:57:57+00:00

Let’s say I have a simple application. It tries to add a new entry

  • 0

Let’s say I have a simple application. It tries to add a new entry to a database grabbing the info from the user. The information is a typical MasterDetail object like an invoice.

  1. The Invoice object used to collect the data has some simple attributes (Id, Date, etc…) and a List of InvoiceDetail, consisting of some simple attributes (invoiceId, productId, quantity, etc…)
  2. A form in the UI Layer collects the data from the user, creates an instance of Invoice and sends it to the Business Layer. It has some simple controls for the simple atributes and a DataGridView for the InvoiceDetails list.
  3. The Business layer sends the data to a Validation Module. If any error in the input is detected it has to return some kind of error code.
  4. The Business Layer has to send the error code back to the Form to let the user correct the wrong information.

I want the application to display some kind of warning (a different background color for example) in every form control with an error.

  • What’s a good approach to manage the errors to be able to inform the form which controls are wrong?
  • Can I create an error object?
  • Use delegates?

The harder problem I see is locating a specific item in the details since this list is are arbitrary long, but even without this problem I have no idea where to start.

I’m using VB.net but don’t think this is relevant to the solution of the problem.
P.D: Please suggest me some tags to this post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:57:58+00:00Added an answer on May 20, 2026 at 11:57 am

    If the error is to be notified to the user (so that the data in the form can be corrected) then the best way to notify the user is to use exception rather than using cryptic error codes to denote the error.

    Suppose you have a method in the Validation module that accepts the instance of Invoice, then the code in the Validate() method should be something like this,

    Approach 1: Break when first encounter of the exception

    public void Validate(Invoice invoiceInstance)
    {
        foreach(InvoiceDetail invoiceDetail in invoiceInstance.InvoiceDetailsList)
        {
            if(invoiceDetail.quantity < 0)
            {
                throw new InvoiceDetailException("The invoice detail quantity is less than zero", InvoiceDetailExceptionType.Quantity, invoiceDetail);
            }
    
            // Validate other details
        }  
    }
    

    and in the UI, catch the exception and handle it,

    try
    {
        businessLayer.Validate();
    }
    catch(InvoiceDetailException invoiceDetailException)
    {
        MessageBox.Show(invoiceDetailException.Message);
    }
    

    The custom InvoiceException can be created like this,

    [Serializable]
    public class InvoiceDetailException : Exception
    {
        public InvoiceDetailExceptionType ExceptionType {get; private set;}
    
        public InvoiceDetail Detail {get; private set; }
    
        public InvoiceDetailException () { }
    
        public InvoiceDetailException (string message, InvoiceDetailExceptionType exType, InvoiceDetail detail) : base(message) 
        {
            this.ExceptionType = exType;
            this.Detail = detail;
        }
    
        public InvoiceDetailException (string message, Exception inner) : base(message, inner) { }
    
        protected InvoiceDetailException (
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
    

    And the InvoiceDetailExceptionType enumeration is,

    public enum InvoiceDetailExceptionType
    {
        Quantity,
        ProductId
    }
    

    Approach 2: Show cumulative exceptions
    The entity classes,

    class Invoice
    {
        public int Id { get; set; }
        public List<InvoiceDetail> Details { get; set; }
    
        public Invoice()
        {
            Details = new List<InvoiceDetail>();
        }
    }
    
    class InvoiceDetail
    {
        public int InvoiceId
        { get; set; }
    
        public int Quantity
        { get; set; }
    
        public int ProductId
        { get; set; }
    }
    

    The validator

    static class InvoiceValidator
    {
        public static void Validate(Invoice invoice)
        {
            List detailsException = new List<InvoiceDetailException>();
    
            foreach (InvoiceDetail detail in invoice.Details)
            {
                try
                {
                    ValidateDetail(detail);
                }
                catch (InvoiceDetailException detailException)
                {
                    detailsException.Add(detailException);
                }
            }
    
            InvoiceException invoiceException = null;
    
            if (invoice.Id < 0)
            {
                // If error in the invoice field
                 invoiceException = new InvoiceException("InvoiceId is invalid");
            }
    
            // Validate other fields of invoice...
    
            if (detailsException.Count > 0)
            {
                // If error in the details list
                if (invoiceException == null)
                {
                    // If there is no error in the invoice field, then error only in details
                    invoiceException = new InvoiceException("Exception only in details", detailsException);
                }
                else
                {
                    // If there is error in invoice field as well as details
                    invoiceException.DetailExceptionList = detailsException;
                }
            }
    
            if (invoiceException != null)
            {
                // If there is an error then throw exception
                throw invoiceException;
            }
        }
    
        private static void ValidateDetail(InvoiceDetail detail)
        {
            if (detail.Quantity <= 0)
            {
                throw new InvoiceDetailException("Quantity is invalid", InvoiceDetailExceptionType.Quantity, detail.InvoiceId, detail.ProductId);
            }
    
            // Validate other fields of invoice details...
        }
    }
    

    The custom exceptions,

    [Serializable]
    public class InvoiceDetailException : Exception
    {
        public InvoiceDetailExceptionType DetailExceptionType { get; private set; }
        public int InvoiceId { get; private set; }
        public int ProductId { get; private set; }
    
        public InvoiceDetailException() { }
    
        public InvoiceDetailException(string message, InvoiceDetailExceptionType exceptionType, int invoiceId, int productId)
            : base(message)
        {
            this.DetailExceptionType = exceptionType;
            this.InvoiceId = invoiceId;
            this.ProductId = productId;
        }
    
        public InvoiceDetailException(string message, Exception inner) : base(message, inner) { }
    
        protected InvoiceDetailException(
        System.Runtime.Serialization.SerializationInfo info,
        System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
    
    [Serializable]
    public class InvoiceException : Exception
    {
        public List<InvoiceDetailException> DetailExceptionList { get; set; }
        public InvoiceException() { }
    
        public InvoiceException(string message): base(message) { }
    
        public InvoiceException(string message, List detailsExceptions) : base(message)
        {
            DetailExceptionList = detailsExceptions;
        }
    
        public InvoiceException(string message, Exception inner) : base(message, inner) { }
    
        protected InvoiceException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
    

    The exception type enumeration,

    public enum InvoiceDetailExceptionType
    {
        Quantity,
        ProductId
    }
    

    And finally the call to the validator,

    try
    {
        InvoiceValidator.Validate(invoice);
    }
    catch (InvoiceException invoiceException)
    {
        MessageBox.Show(invoiceException.Message);
        if (invoiceException.DetailExceptionList != null && invoiceException.DetailExceptionList.Count > 0)
        {
            Console.WriteLine("Exception in details :");
            foreach (InvoiceDetailException detailException in invoiceException.DetailExceptionList)
            {
                string printMessage = string.Format("{0}, InvoiceId = {1}, ProductId = {2}", detailException.Message, detailException.InvoiceId, detailException.ProductId);
                Console.WriteLine(printMessage);
            }
        }
    
        // If you want the caller to further handle the exception
        //throw;
    }
    

    More information

    • Exception handling
    • Exception Handling Best Practices in .NET
    • Custom Exception Handling in C#
    • Designing Custom Exceptions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do i
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan
Let's say that I'm currently designing an application where I will need to use
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a class like this: public class Person { private String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.