for some reason my code is resulting to syntax error INSERT INTO and its pointing to executenonquery line

try
                {
                    OleDbCommand cmd = new OleDbCommand(String.Concat("INSERT INTO CHILDREN2 WHERE (Emp_ID , Child_Name, Age ) VALUES ('", Convert.ToInt32(CaptionText.ToString()), "','", txtName.Text, "','", txtAge, ")"), clsData.con);
                    clsData.con.Close();
                    clsData.con.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                    clsData.con.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

Recommended Answers

All 2 Replies

Change this line of code (sql query) into:

OleDbCommand cmd = new OleDbCommand(Stirng.Format("INSERT INTO CHILDREN2 (Emp_ID , Child_Name, Age) VALUES ({0}, {1}, {2})", Convert.ToInt32(CaptionText.Text),txtName.Text, txtAge), clsData.con);

Do you have a WHERE clause too? I removed it from your query, becasue it didnt belonge on that place where you put it.
If you will use a Where clause then create an sql query this was:

"INSERT INTO TableName (Field1, Field2) VALUES (@paramField1, @paramField2) WHERE Field3 = @paramField3"

where params are parameters passed to the fields, and WHERE is a conditional clause, and has to be stated on the end (after specifying parameters).

OleDbCommand cmd = new OleDbCommand(String.Concat("INSERT INTO CHILDREN2 (Emp_ID , Child_Name, Age ) VALUES ('", Convert.ToInt32(CaptionText.ToString()), "','", +txtName.Text+, "','", +txtAge+, ")"), clsData.con);

if ur EmpId is an int, u dnt have to pass it in quotes, take the query string and check with sql db, u ll know where u r going wrong, how ever, use '+' operator to append the values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.