-2

I Have a Problem about C# Parameter,

And I Get This Error:

No OverLoad for method 'ExecuteInsert' takes '5'

This is My Code:

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);
    }
}
}
3
  • 2
    It's very clear that your function takes 6 arguments instead of 5.
    – Alvin Wong
    Feb 1, 2013 at 2:44
  • 1
    You have declared the array size as 5 and added 6 parameters. Will that make a difference Feb 1, 2013 at 2:44
  • 1
    @Soundararajan that'll be the next problem once this code compiles
    – lockstock
    Feb 1, 2013 at 3:02

1 Answer 1

5

Are you kidding? Count your params, notice you are missing string MemberStatus when making the call. The error couldn't be more clear

2
  • So I Need To have another textbox for my string MemberStatus.. Thanks.
    – KamoteQue
    Feb 1, 2013 at 3:07
  • Or do i just need to delete my String MemberStatus and just keep my parameters..
    – KamoteQue
    Feb 1, 2013 at 3:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.