Reputation: 470
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?
Upvotes: 0
Views: 194
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