0

Every one i write code that insert some data into Microsoft Access database but i have an error "Syntax error in insert into statement" i don't know why !!! Any one help me ? thanks in advance ; code:

 OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data  Source=D:\me\Library Store\Library Store\Store.accdb");
    try
    {
        conn.Open();

        OleDbCommand cmd = new OleDbCommand();
        cmd.Connection = conn;
        cmd.CommandText = "INSERT INTO Libarary ( ISBN, Name, Gategory, Author, Cost, Date) VALUES ( @ISBN, @Name, @Gategory, @Author, @Cost, @Date) ";
        cmd.Parameters.AddWithValue("@ISBN", ISBNTB.Text);
        cmd.Parameters.AddWithValue("@Name", NameTB.Text);
        cmd.Parameters.AddWithValue("@Gategory", GategoryTB.Text);
        cmd.Parameters.AddWithValue("@Author", AuthorTB.Text);
        cmd.Parameters.AddWithValue("@Cost", int.Parse(CostTB.Text));
        cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Text);

        cmd.ExecuteNonQuery();

            MessageBox.Show("Data Added!");
            conn.Close();


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

enter image description here

1 Answer 1

6

When one or more of your fields use a reserved keyword you need to enclose ALWAYS that field in square brackets. (A very annoying problem). In your query, you use two reserved keywords: DATE and NAME

cmd.CommandText = "INSERT INTO Libarary ( ISBN, [Name], Gategory, Author, Cost, [Date]) " + 
                  "VALUES ( @ISBN, @Name, @Gategory, @Author, @Cost, @Date) ";

If it is not too late, I suggest you to rename these fields to avoid this kind of problem in future.

Sign up to request clarification or add additional context in comments.

Comments

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.