user5278314
user5278314

Reputation:

insert data in MS Access Database

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();

Upvotes: 0

Views: 62

Answers (1)

Topman
Topman

Reputation: 336

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.

Upvotes: 1

Related Questions