0

I'm trying to create a c# application using N- Tier architecture. This is my business layer method to insert a record in database.

public static bool AddEmployeeBL(Employee newEmployee)
        {
            bool employeeAdded = false;
            try
            {
                if(ValidateEmployee(newEmployee))
                {
                    EmployeeDAL employeeDAL = new EmployeeDAL();
                    employeeAdded = employeeDAL.AddEmployeeDAL(newEmployee);
                }
            }
            catch (MyException ex)
            {
                throw ex;
            }
            
catch (Exception ex)
            {
                throw ex;
            }

            return employeeAdded;
        }

The exception occurs at the throw ex statement in last catch block.

The DAL class method is as follows, here's the method to add data.

public bool AddEmployeeDAL(Employee newEmployee)
        {
            bool employeeAdded = false;

        try
        {
            DbCommand command = DataConnection.CreateCommand();
            command.CommandText = "uspAddEmployee";

            DbParameter param = command.CreateParameter();
            param.ParameterName = "@UserId";
            param.DbType = DbType.Int32;
            param.Value = newEmployee.UserId;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Name";
            param.DbType = DbType.String;
            param.Value = newEmployee.Name;
            command.Parameters.Add(param);

            int affectedRows = DataConnection.ExecuteNonQueryCommand(command);

            if (affectedRows > 0)
            {
                employeeAdded = true;
            }

        }
        catch (DbException ex)
        {
            string errorMessage = string.Empty;                
                    errorMessage = ex.Message;
            
            throw new MyException(errorMessage);
        }
        return employeeAdded;
    }

The configuration class: public class MyConfiguration

static string providerName;

private static string connectionString;
public static string ConnectionString { get => connectionString; set => connectionString = value; }
public static string ProviderName { get => providerName; set => providerName = value; }

public MyConfiguration()
{
    providerName = ConfigurationManager.ConnectionStrings["Connection"].ProviderName;
    connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
}

}

Ritu
  • 1
  • 2
  • Turn on First Chance Exceptions and see what's really being thrown. And here is the reason why not to catch exceptions and rethrow them like that. If you are not dealing with an exception, **don't catch it at all**, if you must rethrow then just `throw;` – Charlieface Feb 28 '21 at 03:55
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) And yes I know it's not the exact same exception – Charlieface Feb 28 '21 at 03:56

1 Answers1

0

Just change the public constructor to static, thus it'll fetch the connection details.

static TravelBookingConfiguration()
        {
            providerName = ConfigurationManager.ConnectionStrings["travelBookingConnection"].ProviderName;
            connectionString = ConfigurationManager.ConnectionStrings["travelBookingConnection"].ConnectionString;
        }
Ritu
  • 1
  • 2