I wrote a C# code called parent. When i run the program and try to add parent details to the database( microsoft access 2010), it gives me this error:

Exception in DBHandlerSystem.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at AdminOffice._Default.Button1_Click(Object sender, EventArgs e) 

here is what i wrote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

namespace AdminOffice
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\...\reg.accdb"; // put your path
            OleDbConnection myConnection = new OleDbConnection(connString);

            string myQuery = "INSERT INTO Parent( Name, Surname, Address, Postcode, Tel number, Email, UserName, Password) VALUES ( '" + TextBox1.Text + "' , '" + TextBox2.Text + "' , '" + TextBox3.Text + "' , '" + TextBox4.Text + "' , '" + TextBox5.Text + "' , '" + TextBox6.Text + "' , '" + TextBox7.Text + "' , '" + TextBox8.Text + "')";
            OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);

            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                Label1.Text = "successful registration";
            }
            catch (Exception ex)
            {
                Label1.Text = "Exception in DBHandler" + ex;
            }
            finally
            {
                myConnection.Close();
            }

        }
    }
}

Tel number has a space in the column name, invalidating your query. You need to use quotes around it (although am not sure which).

Edited 3 Years Ago by pritaeas

Well, the error says that your query is wrong, so that's a good place to start. ;) My guess would be because Access is having trouble parsing "Tel number", and surrounding it with brackets would fix the problem:

string myQuery = "INSERT INTO Parent( Name, Surname, Address, Postcode, [Tel number], Email, UserName, Password) VALUES ( '" + TextBox1.Text + "' , '" + TextBox2.Text + "' , '" + TextBox3.Text + "' , '" + TextBox4.Text + "' , '" + TextBox5.Text + "' , '" + TextBox6.Text + "' , '" + TextBox7.Text + "' , '" + TextBox8.Text + "')";

This is assuming "Tel number" actually has embedded whitespace in the database schema. Otherwise it's a typo and you need to remove that space. I mention this as an alternative because UserName doesn't have a space, which suggests either an inconsistent schema definition or a typo in the C# code.

But typically when getting errors like this I'll trace the query or print out the end result that gets run, then copy/paste and run it directly from the database. Often it's easier to debug from there than simply going by the stack trace in C#.

Comments
Brackets... I should've known.
This article has been dead for over six months. Start a new discussion instead.