2

I am trying to insert data into an Access database with C#. The error I receive is

Insert Into Statement syntax error

The code is

textBox5.Text = ins; //string value
string into = "INSERT INTO customersales(c-invoiceNo,c-invoiceDate, c-discount, c-rant,c-paid, c-due) values ('" +textBox5.Text.ToString() + "'," + dateTimePicker1.Text + "," + diccombo.Text.Trim() + "," + GRANTTXT.Text + "," + TXTPAID.Text + ","+ txtdue.Text + ")";
 con.Open();
 cmd = new OleDbCommand(into, con);
 cmd.ExecuteNonQuery();
 con.Close();

Table name : customersales

columnname :

id (autonumber)
c-invoiceNo (text)
c-invoiceDate  (date/time)
c-discount (currency)
c-grant (currency)
c-paid (currency)
c-due (currency)
2
  • Copy and paste the value of into into a query window in MS Access. Does it work? Commented Jun 20, 2014 at 9:33
  • Into is not a reserved word in C sharp? Commented Jun 20, 2014 at 12:09

2 Answers 2

1

You should be using a parameterized query, like this

string into = 
        "INSERT INTO [customersales] ([c-invoiceNo], [c-invoiceDate], [c-discount], [c-grant], [c-paid], [c-due]) " +
        "VALUES (?,?,?,?,?,?)";
con.Open();
cmd = new OleDbCommand(into, con);
cmd.Parameters.AddWithValue("?", textBox5.Text);
cmd.Parameters.AddWithValue("?", dateTimePicker1.Value.Date);
cmd.Parameters.AddWithValue("?", diccombo.Text);
cmd.Parameters.AddWithValue("?", GRANTTXT.Text);
cmd.Parameters.AddWithValue("?", TXTPAID.Text);
cmd.Parameters.AddWithValue("?", txtdue.Text);
cmd.ExecuteNonQuery();
con.Close();
Sign up to request clarification or add additional context in comments.

2 Comments

this code is not working properly and same error occurred. please suggest again.
@user2834203 I have updated my answer. (Changed dateTimePicker1.Value to dateTimePicker1.Value.Date.)
0

Change your query in this way,

  string into = "INSERT INTO customersales(c-invoiceNo,c-invoiceDate, c-discount, c-grant,c-paid, c-due) values ('" +textBox5.Text.ToString() + "'," + dateTimePicker1.Text + "," + diccombo.Text.Trim() + "," + GRANTTXT.Text + "," + TXTPAID.Text + ","+ txtdue.Text + ")";

Before this, Check whether your autonumber's identity specification sets to true or not ?

1 Comment

Check with my answer now. Change your syntax with my above line of code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.