zzwyb89
zzwyb89

Reputation: 470

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.

enter image description here

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

Upvotes: 0

Views: 194

Answers (1)

Yuriy Galanter
Yuriy Galanter

Reputation: 39777

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.

Upvotes: 2

Related Questions