i have trouble inserting data from textbox into ms access database, I get an error "Syntax error in INSERT INTO.
"
Can someone help me out please? here's the code:
public void button1_Click(object sender, EventArgs e)//save
{
using (OleDbConnection conn = new
OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\productdb.mdb"))
{
OleDbCommand CmdSql = new OleDbCommand("Insert into [product](Kod, names,
price,type,volume,manufacturer,importer)
enter code here
{
conn.Open();
CmdSql.Parameters.AddWithValue("@Kod", textBox1.Text);
CmdSql.Parameters.AddWithValue("@names", textBox2.Text);
CmdSql.Parameters.AddWithValue("@price", textBox3.Text);
CmdSql.Parameters.AddWithValue("@type", textBox4.Text);
CmdSql.Parameters.AddWithValue("@volume", textBox5.Text);
CmdSql.Parameters.AddWithValue("@manufacturer", textBox6.Text);
CmdSql.Parameters.AddWithValue("@importer", textBox7.Text);
CmdSql.ExecuteNonQuery();// i get the error here<<<
conn.Close();
}
}
Add a record to a table or form. Open the table in Datasheet View or the form in Form View. On the Home tab, in the Records group, click New, or click New (blank) record, or press Ctrl+Plus Sign (+). Find the record with an asterisk in the record selector, and enter your new information.
INSERT INTO SELECT inserts into an existing table. SELECT INTO creates a new table and puts the data in it. All of the columns in the query must be named so each of the columns in the table will have a name. This is the most common mistake I see for this command.
Click the row in the query design grid just below where you want the criteria row to appear, and then on the Design tab, in the Query Setup group, click Insert Rows. Access adds a new row above the row that you clicked.
Basic INSERT syntax Here is the basic syntax for adding rows to a table in SQL: INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc); The first line of code uses the INSERT statement followed by the name of the table you want to add the data to.
You are missing the VALUES
portion of your insert statement:
OleDbCommand CmdSql = new OleDbCommand("Insert into [product] (Kod, [names], price, type, volume, manufacturer, importer) VALUES (@Kod, @names, @price, @type, @volume, @manufacturer, @importer)", conn);
And you are using Access and OldeDbCommand... so you actually need to use ?
instead of a named parameter:
OleDbCommand CmdSql = new OleDbCommand("Insert into [product] (Kod, [names], price, type, volume, manufacturer, importer) VALUES (?, ?, ?, ?, ?, ?, ?)", conn);
See this question for more information.
A side note: Ensure you wrap any reserved keywords in square brackets.
The word NAMES is a reserved keyword for MS-Access Jet SQL, you need to enclose it in square brackets. This is the cause of the Syntax error received. (Of course, assuming that the missing VALUES part is just a typo). So the correct syntax is:
OleDbCommand CmdSql = new OleDbCommand("Insert into [product] (Kod, [names],price,type," +
"volume,manufacturer,importer) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)";
I have changed the placeholders for the parameters with a single question mark. OleDb doesn't support the named parameters and just a question mark will do, but, it is of uttermost importance to add the parameters to the OleDbCommand in the exact sequence expected by the command.
There is another aspect of you code that need to be addressed. You use the method AddWithValue
to build your parameter list. This means that the datatype of the parameter is implicitly derived by the datatype of the value. You use everywhere TextBox.Text
and this is a string. So this could cause problems with the update when the receiving field is of a different type (for example Price is probably numeric) If the database field are not of text type then add an appropriate conversion to the incoming parameter value.
For example:
// Supposing you have an in place validator for the text to be converted......
CmdSql.Parameters.AddWithValue("@price", Convert.ToDecimal(textBox3.Text));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With