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!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
MrrrLuiigii
  • 41
  • 2
  • 10
  • 2
    What database are you connecting to? Try using `values(?, ?)` syntax. – Alex K. Nov 12 '18 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. – Marc Gravell Nov 12 '18 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! – Marc Gravell Nov 12 '18 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! – MrrrLuiigii Nov 12 '18 at 21:52

1 Answers1

1

Change your sql to

sql = "insert into player(Username, Password) values(?, ?)"
Peter Karman
  • 111
  • 9