简体   繁体   中英

No value given for one or more required parameters while trying to add a record

I am trying to add a new record into a table of the database, based on textbox input. However, I am getting an error:

No value given for one or more required parameters

I have this code:

    private void addWord_Click(object sender, EventArgs e)
    {
        try
        {
            using (OleDbConnection conn = new OleDbConnection(access7ConnectionString))
            {
                conn.Open();

                using (OleDbCommand cmd = new OleDbCommand("INSERT INTO Words VALUES(" + "@Name)", conn))
                {
                    cmd.Parameters.AddWithValue("@Name", Word.Text);
                    int rows = cmd.ExecuteNonQuery();

                    //rows number of record got inserted
                }
            }
        }
        catch (OleDbException ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

From what I read, ID does not need to be defined if the table's primary key is being auto-incremented. So here is the table in question in Access.

在此输入图像描述

If I give it the ID parameter, it throws up an error that it cannot allow duplicates. How do I fix this?

Try this for your query

"INSERT INTO Words (Word) VALUES(?)"

This would specifically indicate field to insert into and question mark is a placeholder for parameters when dealing with OleDB connection.

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