Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
when i am insert data so i am getting an error like this
C#
Additional information: Syntax error (missing operator) in query expression '@Phone No'.


What I have tried:

C#
for (int i = 0; i < dataGridView21.Rows.Count; i++)
                {

                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connection;
                    command.CommandText = @"insert into Total ([Column1],[Column2],[Column3],[Date],[Receipt No],[Delivery Person],[Report],[Flavours],[Name],[Phone No])
                    VALUES(@Column1, @Column2, @Column3, @Date, @ReceiptNo, @DeliveryPerson, @Report, @Flavours, @Name , @Phone No)";
                    //values('" + dataGridView21.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView21.Rows[i].Cells[1].Value.ToString() + "','" + txtDisplay.Text + "','" + label4.Text + "','" + label2.Text + "','" + label128.Text + "'," + dataGridView21.Rows[i].Cells[0].Value.ToString() + ",'" + dataGridView21.Rows[i].Cells[3].Value.ToString() + "','" + dataGridView21.Rows[i].Cells[5].Value.ToString() + "','" + dataGridView21.Rows[i].Cells[6].Value.ToString() + "');";
                    connection.Open();
                    for (int j = 0; j < dataGridView21.Rows.Count; j++)
                    {
                        var row = dataGridView21.Rows[j];
                        if (row.IsNewRow) continue;

                        command.Parameters.Clear();
                        command.Parameters.AddWithValue("@Column1", row.Cells[0].Value);
                        command.Parameters.AddWithValue("@Column2", row.Cells[1].Value);
                        command.Parameters.AddWithValue("@Column3", txtDisplay.Text);
                        command.Parameters.AddWithValue("@Date", label4.Text);
                        command.Parameters.AddWithValue("@ReceiptNo", label2.Text);
                        command.Parameters.AddWithValue("@DeliveryPerson", label128.Text);
                        command.Parameters.AddWithValue("@Report", row.Cells[0].Value);
                        command.Parameters.AddWithValue("@Flavours", row.Cells[3].Value);
                        command.Parameters.AddWithValue("@Name", row.Cells[5].Value);
                        command.Parameters.AddWithValue("@Phone No", row.Cells[6].Value);
                        command.ExecuteNonQuery();
                    } connection.Close();
                }
                //printreceiptod();
                flpCategories.Enabled = false;
                flpProducts.Enabled = false;
                listBox20.Focus();
                MessageBox.Show("Inserted Sucessfully", dataGridView21.Rows.Count + "   " + "ITEMS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                listBox20.Focus();
                dataGridView21.Rows.Clear();
                label128.Text = "Delivery";
                while (listBox20.Items.Count > 0)
                {
                    button64.PerformClick();
                }
                flpCategories.Enabled = true;

            }
Posted
Updated 20-Dec-16 13:42pm

You cannot put a space in a variable name. You have:
C#
   @Report, @Flavors, @Name , @Phone No)";
.
. and
. 
command.Parameters.AddWithValue("@Phone No", row.Cells[6].Value);

Remove the space from "Phone No".
 
Share this answer
 
Comments
Member 9983063 20-Dec-16 19:31pm    
Thank you so much
Try to replace:
SQL
VALUES(@Column1, @Column2, @Column3, @Date, @ReceiptNo, @DeliveryPerson, @Report, @Flavours, @Name , @Phone No)

with
SQL
VALUES(@Column1, @Column2, @Column3, @Date, @ReceiptNo, @DeliveryPerson, @Report, @Flavours, @Name , \"@Phone No\")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900