错误:“没有OverLoad方法'ExecuteInsert'取'5'参数”

时间:2013-02-01 02:42:16

标签: c# sql sql-server

我有关于C#参数的问题,

我收到此错误:

  

没有OverLoad for method' ExecuteInsert'需要' 5'

这是我的代码:

private void ExecuteInsert(string FirstName, string LastName, string MiddleName, string UserName, string Password, string MemberStatus)
{
    SqlConnection conn = new SqlConnection(GetConnectionString());
    string sql = "INSERT INTO Registration (FirstName, LastName, MiddleName, UserName, Password, MemberStatus, DateRegistration) VALUES"
                + " (@FirstName,@LastName,@MiddleName,@UserName,@Password,'ACTIVE',CURRENT_TIMESTAMP)";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlParameter[] param = new SqlParameter[5];
        param[0] = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
        param[1] = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
        param[2] = new SqlParameter("@MiddleName", SqlDbType.VarChar, 50);
        param[3] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
        param[4] = new SqlParameter("@Password", SqlDbType.VarChar, 50);
        param[5] = new SqlParameter("ACTIVE", SqlDbType.VarChar, 50);
        param[6] = new SqlParameter("CURRENT_TIMESTAMP", SqlDbType.VarChar, 50);

        param[0].Value = FirstName;
        param[1].Value = LastName;
        param[2].Value = MiddleName;
        param[3].Value = UserName;
        param[4].Value = Password;
        param[5].Value = MemberStatus;
        param[6].Value = DateRegistration;

        for (int i = 0; i < param.Length; i++)
        {
            cmd.Parameters.Add(param[i]);
        }

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex)
    {
        string msg = "Insert Error:";
        msg += ex.Message;
        throw new Exception(msg);
    }
    finally
    {
        conn.Close();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    if (TxtPassword.Text == TxtRePassword.Text)
    {
        //call the method to execute insert to the database
        ExecuteInsert(TxtFName.Text,
                      TxtLName.Text,
                      TxtMName.Text,
                      TxtUserName.Text,
                      TxtPassword.Text);
        Response.Write("Record was successfully added!");
        ClearControls(Page);
    }
    else
    {
        Response.Write("Password did not match");
        TxtPassword.Focus();
    }
}
public static void ClearControls(Control Parent)
{
    if (Parent is TextBox)
    { (Parent as TextBox).Text = string.Empty; }
    else
    {
        foreach (Control c in Parent.Controls)
            ClearControls(c);
    }
}
}

1 个答案:

答案 0 :(得分:5)

你在开玩笑吧?计算你的参数,通知你在打电话时遗漏string MemberStatus。错误无法更清楚