Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a checedlist box - of which if the user checks a box and clicks the 'send' button a new row is created in the database (using the item checked)with the required information. But when I run it I keep on getting an error when I debug it saying ...

Syntax error in INSERT INTO statement


Can anyone help, I cant find the error


private void Send_Click(object sender, EventArgs e)
{
    string err = "";
    if (checkedListBox.Items.Count > 0)
    {
        for (int i = 0; i < checkedListBox.Items.Count; i++)
        {
            if (err == "")
            {
                err = checkedListBox.Items[i].ToString();
            }

            MAcon.Open();
            OleDbCommand cmd = new OleDbCommand("Insert into Scrap([Order ID], Barcode, Notes, [Scrapped By], [Scrapped Date]) value(@OrderID, @Barcode, @Notes, @ScrappedBy, @ScrappedDate)", MAcon);
            cmd.Parameters.AddWithValue("@OrderID", OrderID.Text);
            cmd.Parameters.AddWithValue("@Notes", Note.Text);
            cmd.Parameters.AddWithValue("@ScrappedBy", EmplID.Text);
            cmd.Parameters.AddWithValue("@ScrappedDate", Date.Text);
            cmd.Parameters.AddWithValue("@Barcode", err);
            cmd.ExecuteNonQuery();
            MAcon.Close();
        }
    }
}


What I have tried:

MAcon.Open();
OleDbCommand cmd = new OleDbCommand("Insert into Scrap([Order ID], Barcode, Notes, [Scrapped By], [Scrapped Date]) value(@OrderID, @Barcode, @Notes, @ScrappedBy, @ScrappedDate)", MAcon);
cmd.Parameters.AddWithValue("@OrderID", OrderID.Text);
cmd.Parameters.AddWithValue("@Notes", Note.Text);
cmd.Parameters.AddWithValue("@ScrappedBy", EmplID.Text);
cmd.Parameters.AddWithValue("@ScrappedDate", Date.Text);
cmd.Parameters.AddWithValue("@Barcode", err);
cmd.ExecuteNonQuery();
MAcon.Close();
Posted
Updated 6-May-18 23:42pm
Comments
Richard MacCutchan 7-May-18 5:45am    
Are you sure that all those values are simple VARCHAR fields in your database? Storing Dates as text is a really bad idea to start with.

Please correct the spelling of 'value'.It should be 'values'

OleDbCommand cmd = new OleDbCommand("Insert into Scrap([Order ID], Barcode, Notes, [Scrapped By], [Scrapped Date]) <big>values</big>(@OrderID, @Barcode, @Notes, @ScrappedBy, @ScrappedDate)", MAcon);



SQL INSERT Query[^]
 
Share this answer
 
v2
Comments
Maciej Los 7-May-18 5:39am    
5ed!
In addition to solution #1 by Dominic Abraham[^]

You forgot to add @barcode parameter:
C#
MAcon.Open();
OleDbCommand cmd = new OleDbCommand("Insert into Scrap([Order ID], Barcode, Notes, [Scrapped By], [Scrapped Date]) values(@OrderID, @Barcode, @Notes, @ScrappedBy, @ScrappedDate)", MAcon);
cmd.Parameters.AddWithValue("@OrderID", OrderID.Text);
cmd.Parameters.AddWithValue("@OrderID", Barcode.Text); //<- missed value!
cmd.Parameters.AddWithValue("@Notes", Note.Text);
//...
 
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