类型' System.ArgumentException'的例外情况发生在System.Data.dll中但未在用户代码中处理

时间:2017-02-06 14:51:22

标签: c# stored-procedures

它进一步指出过程或函数spAddOrganization需要未提供的参数Organizational_Number

IEnumerable<OrganizationalDetails> OrganizationalDetails
{
    get
    {
        string connectionString = ConfigurationManager.ConnectionStrings["OnlineDBContext"].ToString();
        List<OrganizationalDetails> organization = new List<OrganizationalDetails>();

        using (SqlConnection con = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand("spAddOrganization", con);
            cmd.CommandType = CommandType.StoredProcedure;`
            con.Open();

            SqlDataReader myreader = cmd.ExecuteReader();

            while(myreader.Read())
            { 
                OrganizationalDetails organize = new OrganizationalDetails();
                organize.Organizational_Id = Convert.ToInt32(myreader["Organizational_Id"].ToString());
                organize.Organizational_Number = myreader["Organizational_Number"].ToString();
                organize.Location = myreader["Location"].ToString();
                organize.Organization_Address = myreader["Organizational_Address"].ToString();
                organize.TelephoneNo_Org = myreader["TelephoneNo_Org"].ToString();
                organize.Organizational_Name = myreader["Organizational_Name"].ToString();`ganizational_Name = myreader["Organizational_Name"].ToString();_Name = myreader["Organizational_Name"].ToString();
                organize.Administrator_Name = myreader["Administrator_Name"].ToString();
                organization.Add(organize); 
            }
        }
        return organization;
    }
}

存储过程如下:

ALTER PROCEDURE [dbo].[spAddOrganization]
    @Organizational_Number nvarchar(50),
    @Location nvarchar(50),
    @Organizational_Address nvarchar(50),
    @TelephoneNo_Org nvarchar(50),
    @Organizational_Name nvarchar(50),
    @Administrator_Name nvarchar(50)
AS
BEGIN
    INSERT INTO Organization_Details(Organizational_Number,Location,Organizational_Address,TelephoneNo_Org,Organizational_Name,Administrator_Name)
    VALUES(@Organizational_Number,@Location,@Organizational_Address,@TelephoneNo_Org,@Organizational_Name,@Administrator_Name)
END

一切似乎都很完美。我试过谷歌搜索,尝试但发现不是真正可以理解的答案。

1 个答案:

答案 0 :(得分:0)

因为您的Stored Procerure有一个Insert子句,您需要传递这样的参数:

string connectionString = ConfigurationManager.ConnectionStrings["OnlineDBContext"].ToString();

using (SqlConnection con = new SqlConnection(connectionString))
{

    SqlCommand cmd = new SqlCommand("spAddOrganization", con);
    con.Open();
    cmd.CommandType = CommandType.StoredProcedure;

    SqlParameter[] parameters = new SqlParameter[]
    {
        new SqlParameter("@Organizational_Number", organize.Organizational_Number),
        new SqlParameter("@Location", organize.Location),
        new SqlParameter("@Organizational_Address", organize.Organization_Address),
        new SqlParameter("@TelephoneNo_Org", organize.TelephoneNo_Org),
        new SqlParameter("@Organizational_Name", organize.Organizational_Name),
        new SqlParameter("@Administrator_Name", organize.Administrator_Name)
    };
    cmd.Parameters.AddRange(parameters);
    int retorno = cmd.ExecuteNonQuery();
}