user8930129
user8930129

Reputation:

How to insert data into MS Access table without entering data into the AutoNumber column?

I am working with Microsoft Access 2016 and developing a ASP.NET WebForm where I want to enter data into the tbl_users table. I have the following server side code:

connectionString = GetConnString();
string insertQuery = "insert into tbl_users (fullName, emailID, password) values(?, ?, ?)";
using (OleDbConnection con = new OleDbConnection(connectionString))
{
    con.Open();
    using (OleDbCommand insertCommand = new OleDbCommand(insertQuery, con))
    {
        insertCommand.CommandType = CommandType.Text;
        insertCommand.Parameters.AddWithValue("fullName", TextBoxFullName.Text);
        insertCommand.Parameters.AddWithValue("emailID", TextBoxEmailID.Text);
        insertCommand.Parameters.AddWithValue("password", TextBoxPassword.Text);        
        insertCommand.ExecuteNonQuery();
        con.Close();
    }
}

The table schemas is as follows:

tbl_users(userID(AutoNumber), fullName(LongText), emailID(LongText), password(LongText))

When I run the code, I get the following error:

System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement

Where am I going wrong?

Upvotes: 1

Views: 82

Answers (1)

Erik A
Erik A

Reputation: 32642

password is a keyword in Access SQL, thus needs to be bracketed. Add brackets ([password]) and your query will run fine.

End result:

string insertQuery = "insert into tbl_users (fullName, emailID, [password]) values(?, ?, ?)";

Upvotes: 1

Related Questions