0

my database in MS Access.i have a table named Admin.It has two attributes Username and Password. when i use insert query for inserting data then it show a message"syntax error in INSERT INTO statement." but i could not find out the problem . please help my code-

string cb = "INSERT INTO Admin(Username,Password) VALUES (@d1,@d2)";

command = new OleDbCommand(cb);

command.Connection = connection;

command.Parameters.Add(new OleDbParameter("@d1", System.Data.OleDb.OleDbType.VarChar, 20, "Username"));
command.Parameters.Add(new OleDbParameter("@d2", System.Data.OleDb.OleDbType.VarChar, 30, "Password"));


command.Parameters["@d1"].Value = textusername.Text;
command.Parameters["@d2"].Value = textpassword.Text;



command.ExecuteNonQuery();
MessageBox.Show("Successfully saved", "Student Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
Add.Enabled = true;
if (connection.State == ConnectionState.Open)
{
    connection.Close();
}
connection.Close();
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • 1
    possible duplicate of [using parameters inserting data into access database](http://stackoverflow.com/questions/5893837/using-parameters-inserting-data-into-access-database) – KornMuffin Sep 03 '15 at 16:34

1 Answers1

1

I have gone through the same problem before.

And the problem is that you have used attribute Password which is the reserved word in Ms-Access. So, instead of using Password you can use something like

Pword

And it will work fine.

Visit Reserved words for the list of reserved words. It is the list for Access2007.

Hope it helps.

Topman
  • 306
  • 1
  • 4
  • 14