简体   繁体   中英

expects parameter '@id', which was not supplied. the id is primary key and identity

I get this error:

expects parameter '@id', which was not supplied. the id is primary key and identity

My code:

namespace se_up_de_in
{
    public partial class Form1 : Form
    {
        SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-R0N4ID3;Initial Catalog=DBTask2;Integrated Security=True");

        SqlCommand comm;
        SqlDataAdapter Adapter;

        DataTable Table = new DataTable();

        private void button2_Click(object sender, EventArgs e)
        {
            using (comm = new SqlCommand("proc_insertid", conn))
            {
                comm.CommandType = CommandType.StoredProcedure;
                conn.Open();

                SqlParameter[] parameter = new SqlParameter[5];
                parameter[0] = new SqlParameter("@id", SqlDbType.Int);
                parameter[0].Value = textBox5.Text;

                parameter[1] = new SqlParameter("@username", SqlDbType.NChar);
                parameter[1].Value = textBox1.Text;

                parameter[2] = new SqlParameter("@password", SqlDbType.NChar);
                parameter[2].Value = textBox2.Text;


                parameter[3] = new SqlParameter("@email", SqlDbType.NVarChar);
                parameter[3].Value = textBox3.Text;


                parameter[4] = new SqlParameter("@Type", SqlDbType.NChar);
                parameter[4].Value = textBox4.Text;

                comm.ExecuteNonQuery();

                comm.Parameters.AddRange(parameter);

                conn.Close();

                dataGridviewfill();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (comm = new SqlCommand("proc_Dlelete", conn))
            {
                comm.CommandType = CommandType.StoredProcedure;
                conn.Open();

                SqlParameter param = new SqlParameter();
                param = new SqlParameter("@id", SqlDbType.NChar);
                param.Value = textBox5.Text;

                comm.ExecuteNonQuery();
                comm.Parameters.Add(param);

                conn.Close();

                dataGridviewfill();
            }
        }
    }
}

My stored procedure:

CREATE PROCEDURE proc_insert
     (@id INT,
      @username NCHAR(10),
      @password NCHAR(10),
      @email NVARCHAR(MAX),
      @Type NCHAR(10))
AS
BEGIN
    INSERT INTO ttask2 (id, username, password, email, Type)
    VALUES (@id, @username, @password, @email, @Type)
END
comm.ExecuteNonQuery();
comm.Parameters.AddRange(parameter);

Reverse these; you need to add the parameters before executing the command.

comm.Parameters.AddRange(parameter);
comm.ExecuteNonQuery();

(same for other places you use the command/parameters)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM