locked
Error handling RRS feed

  • Question

  • User1513812574 posted

    Hi

    How can i pass the different types of errors from Data access layer to presentation layer?

    suppose if we take the northwind database

    scenario

    I want to delete the customer, so i selected one customer in ui and clicked the "delete" button.It internally calls the "delete" in data access layer.

    The prerequisite for deleting the customer is that the customer doesn't have any orders.So in data access layer we wil check whether that customer has any orders.If the customer has orders how can we pass the message from dal to presentation layer that the customer has orders and we don't delete.

    Am i doing right?is there any other ways to deal with this type?

    Thanks in advance

    Thursday, June 3, 2010 6:14 AM

Answers

  • User-1618234021 posted

    Hi

    Normally, there is a Business Logic Layer in b/w Presentation and Data Access. Your function can return a string value containing the exception message. You can reuturn the string value in finally block. In the catch block set it to the message of the exception or if everything runs fine, set this to empty string.

    On the presentation layer, check this string value and notify the user.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 3, 2010 7:42 AM
  • User-1237044210 posted

    Hi Kommuri,

    Normally, DAL use try.. catch..finally..., if have any error we will write log and throw error up to next layer. in BLL also do the same with DAL and the UI will display error and processing it. You should define your own Exception with name is a business situation. For example, IdIsNullException, NumberIsLessThanZeroException,... IMHO

    Regards,

    ThangChung

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 3, 2010 12:46 PM

All replies

  • User-1618234021 posted

    Hi

    Normally, there is a Business Logic Layer in b/w Presentation and Data Access. Your function can return a string value containing the exception message. You can reuturn the string value in finally block. In the catch block set it to the message of the exception or if everything runs fine, set this to empty string.

    On the presentation layer, check this string value and notify the user.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 3, 2010 7:42 AM
  • User-1184423958 posted

    Here it is enjoy!

    public class Customer
        {
            public int Id { get; set; }
            public int Name { get; set; }
            public List<Order> Orders { get; set; }
    
            public bool Save()
            {
                //Save customer data in database
            }
    
            public static Customer Get(int id)
            {
                if (id <= 0)
                    throw new ArgumentException("Customer Id cannot be zero");
                //Get customer data from database
                Customer newCustomer = new Customer();
                //Fill customer data into newCustomer 
    
                return newCustomer;
            }
    
            public static void Delete(int id)
            {
                if (id <= 0)
                    throw new ArgumentException("Customer Id cannot be zero");
                if (Get(id).Orders.Count > 0)
                    throw new InvalidOperationException("Customer has orders. It cannot be deleted");
                //Delete customer data from database
                
            }
        }
    
        public class Order
        {
            public int Id { get; set; }
            public int Qty { get; set; }
        }


     

    Thursday, June 3, 2010 8:24 AM
  • User-1237044210 posted

    Hi Kommuri,

    Normally, DAL use try.. catch..finally..., if have any error we will write log and throw error up to next layer. in BLL also do the same with DAL and the UI will display error and processing it. You should define your own Exception with name is a business situation. For example, IdIsNullException, NumberIsLessThanZeroException,... IMHO

    Regards,

    ThangChung

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 3, 2010 12:46 PM