0

Using Access database like this :

OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Request.PhysicalApplicationPath + "Resources/cars_db.accdb");
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO Users (Username, Password, Email, Address, Question, Answer) VALUES ('" + txtUsernameRP.Text + "','" + txtPasswordRP.Text + "','" + txtEmailRP.Text + "','" + txtAddressRP.Text + "','" + txtQuestionRP.Text + "','" + txtAnswerRP.Text + "')";
int i = cmd.ExecuteNonQuery(); -- **Breaks here and says syntax error**

I have tried:

  1. Taking out the int i bit.
  2. Putting the @ symbol in front of the statement.
  3. Checked and made sure that I am using the Access code.
  4. Tried closing the connection before creating in case it was something there.
  5. Substituted the data for fixed values, which I then ran in a query in Access which worked.
  6. Put the access query mentioned above into the code block (cmd.CommandText = "INSERT INTO Users (Username, Password, Email, Address, Question, Answer) VALUES ('asdasd','Asd!23asd','asdasd','asdasd','asdasd','asdasd')"; and tried running it and again same syntax error.

Please someone help me...

12
  • 1
    Do you have any specific syntax error (near line ...) ? Or just general syntax error? Commented Oct 20, 2015 at 9:19
  • 1
    You missed a paranteces in nr 6. i dont know if it matters but... Commented Oct 20, 2015 at 9:19
  • As per your query generated '(' is missing after Users. Commented Oct 20, 2015 at 9:20
  • So, why did you add MySQL tag if you use Access? Commented Oct 20, 2015 at 9:20
  • 1
    Did you try this request outside your code? Commented Oct 20, 2015 at 9:29

3 Answers 3

2

Password is reserved keyword

  cmd.CommandText = "INSERT INTO Users (Username, Password, Email, Address...

try this

cmd.CommandText = "INSERT INTO Users (Username, [Password], Email, Address...
Sign up to request clarification or add additional context in comments.

Comments

0

Can You try this?

 "INSERT INTO Users (Username, Password, Email, Address, Question, Answer) 
    VALUES(
    '" + txtUsernameRP.Text + "',
    '" + txtPasswordRP.Text + "',
    '" + txtEmailRP.Text + "',
    '" + txtAddressRP.Text + "',
    '" + txtQuestionRP.Text + "',
    '" + txtAnswerRP.Text + "');";

1 Comment

I have tried removing "(Username, Password, Email, Address, Question, Answer)" and it works fine for me..
0

I guess your problem is on that int i, try removing it

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.