类型为' System.ArgumentException'的未处理异常发生在BLL.dll中

时间:2015-02-21 21:51:37

标签: c# sql stored-procedures

所以我有一个3层应用程序,使用SQL连接和存储过程添加到数据库中的表。我在我的数据访问层中设置了如下存储过程...

public bool CreateAccount(AccountModel newAccount)
    {
        bool retValue = false;
        try
        {
           using (SqlConnection cxn = new SqlConnection(cxnString))

            {
                SqlCommand cmd = new SqlCommand("spAddAccount", cxn);
                cmd.CommandType = CommandType.StoredProcedure;


                SqlParameter FirstNameParam = new SqlParameter("@FirstName", SqlDbType.NVarChar, 256);
                FirstNameParam.Value = newAccount.FirstName;
                SqlParameter SurnameParam = new SqlParameter("@Surname", SqlDbType.NVarChar, 256);
                SurnameParam.Value = newAccount.Surname;
                SqlParameter EmailParam = new SqlParameter("@Email", SqlDbType.NVarChar, 256);
                EmailParam.Value = newAccount.Email;
                SqlParameter PhoneParam = new SqlParameter("@Phone", SqlDbType.NVarChar, 256);
                PhoneParam.Value = newAccount.Phone;
                SqlParameter Address1Param = new SqlParameter("@Address2", SqlDbType.NVarChar, 256);
                Address1Param.Value = newAccount.Address1;
                SqlParameter Address2Param = new SqlParameter("@Address2", SqlDbType.NVarChar, 256);
                Address2Param.Value = newAccount.Address2;
                SqlParameter CityParam = new SqlParameter("@City", SqlDbType.NVarChar, 256);
                CityParam.Value = newAccount.City;
                SqlParameter CountyParam = new SqlParameter("@County", SqlDbType.NVarChar, 256);
                CountyParam.Value = newAccount.County;
                SqlParameter AccountTypeParam = new SqlParameter("@AccountType", SqlDbType.NVarChar, 256);
                AccountTypeParam.Value = newAccount.AccountType;
                SqlParameter InitialBalanceParam = new SqlParameter("@InitialBalance", SqlDbType.NVarChar, 256);
                InitialBalanceParam.Value = newAccount.InitialBalance;
                SqlParameter OverdraftLimitParam = new SqlParameter("@OverdraftLimit", SqlDbType.NVarChar, 256);
                OverdraftLimitParam.Value = newAccount.OverdraftLimit;
                SqlParameter AccountNumberParam = new SqlParameter("AccountNumber", SqlDbType.Int, 4);
                AccountNumberParam.Direction = ParameterDirection.Output;

                cmd.Parameters.Add(FirstNameParam);
                cmd.Parameters.Add(SurnameParam);
                cmd.Parameters.Add(EmailParam);
                cmd.Parameters.Add(PhoneParam);
                cmd.Parameters.Add(Address1Param);
                cmd.Parameters.Add(Address2Param);
                cmd.Parameters.Add(CityParam);
                cmd.Parameters.Add(CountyParam);
                cmd.Parameters.Add(AccountNumberParam);
                cmd.Parameters.Add(InitialBalanceParam);
                cmd.Parameters.Add(OverdraftLimitParam);
                cmd.Parameters.Add(AccountNumberParam);

                cxn.Open();
                cmd.ExecuteNonQuery();
                cxn.Close();

           }
        }
        catch (Exception)
        {

            throw;
        }
        return retValue;
    }
}

但是当我运行引用我的逻辑层的程序时,我得到了这个错误。 ErrorMessage`

我在查询执行之前和之后都尝试使用cmd.Parameters.Clear()方法,但它没有帮助。

根本没有设置其他连接或SQL命令,这是唯一的连接。

在回到网上搜索后无法弄明白。

由于

这是在业务逻辑层中启动异常的地方......

namespace BLL
{
    public class bllAccountManager
    {
        public bool CreateAccount(AccountModel newAccount)
        {
            bool retValue = false;

            dalAccountManager dal = new dalAccountManager();
            try
            {
                retValue = dal.CreateAccount(newAccount);
            }
            catch (Exception ex)
            {

                throw;
            }
            return retValue;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

AccountNumberParam参数已添加两次。例外是有点自我解释,只需找到罪魁祸首。