程序或功能' spAddEmployee'期望参数' @ name',这是未提供的

时间:2016-12-31 21:37:12

标签: c# asp.net-mvc

我的创建表单需要从存储过程访问,当我运行应用程序时,我收到此错误:

  

程序或功能' spAddEmployee'期望参数' @ name',未提供

C#代码:

string connectionstring = ConfigurationManager.ConnectionStrings["AddEmployee"].ConnectionString;

using (SqlConnection con = new SqlConnection(connectionstring))
{
    SqlCommand cmd = new SqlCommand("dbo.spAddEmployee", con);
    cmd.CommandType = CommandType.StoredProcedure;

    con.Open();

    SqlParameter paramname = new SqlParameter();
    paramname.ParameterName = "@Name";
    paramname.Value = employee.Name;
    cmd.Parameters.Add(paramname);

    SqlParameter paramgender = new SqlParameter();
    paramname.ParameterName = "@gender";
    paramname.Value = employee.Gender;
    cmd.Parameters.Add(paramgender);

    SqlParameter paramcity = new SqlParameter();
    paramname.ParameterName = "@city";
    paramname.Value = employee.City;
    cmd.Parameters.Add(paramcity);

    SqlParameter paramdateofbirth = new SqlParameter();
    paramdateofbirth.ParameterName = "@dateofbirth";
    paramname.Value = employee.Dateofbirth;
    cmd.Parameters.Add(paramdateofbirth);

    cmd.ExecuteNonQuery();

    con.Close();
}

my procedure code is given in snapshot

请帮帮我

1 个答案:

答案 0 :(得分:3)

您使用的是与性别和城市名称相同的SqlParameter对象(paramname)。所以它基本上覆盖了第一个性别和城市的价值。所以我假设您的City值为null,因此它会抛出错误。

这应该有效

using (var con = new SqlConnection(connectionstring))
{
    var cmd = new SqlCommand("dbo.spAddEmployee", con) {
                                CommandType = CommandType.StoredProcedure};
    con.Open();
    var paramname = new SqlParameter
    {
        ParameterName = "@Name",
        Value = employee.Name
    };
    cmd.Parameters.Add(paramname);

    var paramgender = new SqlParameter
    {
        ParameterName = "@gender",
        Value = employee.Gender
    };
    cmd.Parameters.Add(paramgender);

    var paramcity = new SqlParameter
    {
        ParameterName = "@city",
        Value = employee.City
    };
    cmd.Parameters.Add(paramcity);

    var paramdateofbirth = new SqlParameter
    {
        ParameterName = "@dateofbirth",
        Value = employee.Dateofbirth
    };
    cmd.Parameters.Add(paramdateofbirth);
    cmd.ExecuteNonQuery();

}