简体   繁体   English

System.ArgumentNullException: '值不能是 null。 参数名称:providerInvariantName'

[英]System.ArgumentNullException: 'Value cannot be null. Parameter name: providerInvariantName'

I'm trying to create a c# application using N- Tier architecture.我正在尝试使用 N 层架构创建 c# 应用程序。 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.异常发生在最后一个 catch 块中的 throw ex 语句处。

The DAL class method is as follows, here's the method to add data. DAL class方法如下,这里是添加数据的方法。

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

} }

Just change the public constructor to static, thus it'll fetch the connection details.只需将公共构造函数更改为 static,它就会获取连接详细信息。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.ArgumentNullException:'值不能为null。 参数名称:键 - System.ArgumentNullException: 'Value cannot be null. Parameter name: key' System.ArgumentNullException: '值不能为空。 参数名称:items' - System.ArgumentNullException: 'Value cannot be null. Parameter name: items' System.ArgumentNullException: '值不能为空。 参数名称:提供者' - System.ArgumentNullException: 'Value cannot be null. Parameter name: provider' System.ArgumentNullException:“值不能是 null。 参数名称:实体” - System.ArgumentNullException: “Value cannot be null. Parameter name: entity” System.ArgumentNullException:值不能为 null。(参数“connectionString”) - System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') Winform: System.ArgumentNullException: '值不能为空。 (参数'流')' - Winform: System.ArgumentNullException: 'Value cannot be null. (Parameter 'stream')' AspNetCore.Authorization: System.ArgumentNullException: '值不能为 null。(参数 'configure')' - AspNetCore.Authorization: System.ArgumentNullException: 'Value cannot be null. (Parameter 'configure')' System.ArgumentNullException:'值不能为null。' - System.ArgumentNullException: 'Value cannot be null.' 如何修复“ System.ArgumentNullException:'值不能为null。 参数名称:entity'” - How to fix “System.ArgumentNullException: 'Value cannot be null. Parameter name: entity'” Autofac System.ArgumentNullException:值不能为null。 参数名称:上下文 - Autofac System.ArgumentNullException : Value cannot be null. Parameter name: context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM