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;
}
}