-1

The following is the code in UI, BLL and DAL. BLL needs to handle the exception if there is any issue in the executenonquery of DAL and show the user with a generic message. We are using Elmah for error handling.Could anyone please suggest how to handle the exception and show a generic message page(like error 500) to the user.

Controller

refNo = _formServiceWorker.SubmitFormData(formData, isFinalized);

Business Logic Layer:

public string SubmitFormData(GenericFormData formData, bool isFinalized)
{            
        var submissionHeaderDTO = new SubmissionHeaderDTO();
        submissionHeaderDTO = SubmissionHeader(formData.FormId, submissionHeaderVariablesDTO);

        var formFieldDataList = GetFormFieldData(formData, submissionHeaderDTO);

        if (formFieldDataList.Count() > 0)
        {                    
            this._formDataService.DeleteFormData(submissionHeaderDTO.SubmissionId); // calls Data Access Layer

            foreach (var formField in formFieldDataList)
            {                        
                    this._formDataService.SubmitFormData(formField); //Calls Data Access Layer
            }
        }
    }
    return submissionHeaderDTO.SubmitSequence;
}

Data Access Layer

public SubmissionHeaderDTO GetRefNo(SubmissionHeaderVariablesDTO requestVariables)
{
    using(var sqlConn = new SqlConnection(Configuration.DBConnection))
    {
        sqlConn.Open();                
        using (var sqlcmd = new SqlCommand("usp_testSubmissionHeaderInsert", sqlConn))
        {
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Parameters.Add("@FormId", SqlDbType.UniqueIdentifier).Value = requestVariables.FormId;

            SqlParameter outputSubmissionSequence = new SqlParameter("@SubmitSequence", SqlDbType.NVarChar, 30)
            {
                Direction = ParameterDirection.Output
            };

            SqlParameter outputFormGuid = new SqlParameter("@FormIdGuid", SqlDbType.UniqueIdentifier)
            {
                Direction = ParameterDirection.Output
            };

            sqlcmd.Parameters.Add(outputSubmissionSequence);
            sqlcmd.Parameters.Add(outputFormGuid);

            sqlcmd.ExecuteNonQuery();

            var submissionHeaderDto = new SubmissionHeaderDTO()
            {
                SubmissionId = (Guid)outputSubmissionId.Value,
                SubmitSequence = outputSubmissionSequence.Value.ToString(),
                FormId = (Guid)outputFormGuid.Value
            };
            return submissionHeaderDto;
        }
    }
}
ekad
  • 14,436
  • 26
  • 44
  • 46
Rama
  • 73
  • 1
  • 4
  • 14

2 Answers2

0

You will need to wrap the complete code(or just the sqlcmd.ExecuteNonQuery() line as your need is) in the Data Access Layer with a try-catch block and throw a custom exception(eg: custom DataAccessLayerException exception class). Then in your Controller code, you would add a try-catch block, handle the custom exception thrown from the Data Access Layer and show a generic message page(like error 500) to the user.

Ajit Goel
  • 4,180
  • 7
  • 59
  • 107
0

You probably already solved this, but for future reference, I think there's a better solution to this problem. You can indeed wrap your data access layer code in try catch and throw a custom exception with the original exception as inner exception. You can also let the exception flow through to the web framework and let that one handle it.

What you shouldn't do (imo), is to handle db errors in all of your website code and do something custom like redirecting to an error page or simiar. That's what custom error pages are for. It's a very complex subject, but there are some good documentation to setting it up. The best one i've found, is Demystifying ASP.NET MVC 5 Error Pages and Error Logging. Since you are also using ELMAH, you should be aware of a conflict between setting up custom error pages and ELMAH, since the custom error page "swallows" the uncaught exceptions. Luckily, there's a way to fix this. Check out ELMAH and custom errors (a post I've written).

ThomasArdal
  • 4,999
  • 4
  • 33
  • 73