2

I've got an error message:

System.Data.OleDb.OleDbException: 'Syntax error in INSERT INTO statement.'

I've read through multiple questions like this, but non of their answers helped me.

This is my code:

conn.Open();

string sql = "";
sql = "insert into player(Username, Password) values(@Username, @Password)";

using (command = new OleDbCommand(sql, conn))
{
    command.Parameters.AddWithValue("@Username", textBoxUsername.Text);
    command.Parameters.AddWithValue("@Password", textBoxPassword.Text);
    command.ExecuteNonQuery();
}
command.Dispose();
conn.Close();

The error is in the following line:

command.ExecuteNonQuery();

Does anyone know a solution for this error?

Thanks in advance!

4
  • 2
    What database are you connecting to? Try using values(?, ?) syntax. Commented Nov 12, 2018 at 16:08
  • 1
    OleDb is a very general purpose API that can talk to multiple databases; if you're talking to a specific database, it is usually preferable to use that database's specific API - for example, SqlConnection/SqlCommand etc for SQL Server. This will allow you to use that target database's full set of features, such as named parameters. OleDb is quite... well, minimal, frankly. It doesn't support named parameters, as noted by the existing answer etc. Commented Nov 12, 2018 at 16:16
  • 2
    BTW: good job on using parameters in the first place; you'd be amazed how often we see SQL concatenation of values! Commented Nov 12, 2018 at 16:17
  • I'm using an Acces database and thus SqlCommand does not work for me. The targetting of the database is not the problem since reading from it works perfectly well. I discoverd my problem was the use of "Password" as column name. Changing this to "Passw" solved it. Thanks for the help anyway! Commented Nov 12, 2018 at 21:52

1 Answer 1

1

Change your sql to

sql = "insert into player(Username, Password) values(?, ?)"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.