Search code examples
asp.netdatabasems-accessinsert-into

Asp.net Insert Into query local database (acess)


I am not very good at running sql queries to a local database - Microsoft Access. I am trying to write a code where when a user register their details, it insert it into the database. I have wrote the following code but it's not working. I think I have missed out something. Any help will be appreciated.

 public void InsertUserDetails(string fname, string sname, string uid, string password, string email)
{
    //Connection string for the datbase
    string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/Forum.accdb;Persist Security Info=True";
    OleDbConnection myConn = new OleDbConnection(database);


    //Execute the query
    string queryStr = "Insert into Users(Username, Password, Firstname, Surname, Email) values ('" + fname + "','" + sname + "','" + uid + "','" + password + "','" + email + "')";

     // Create a command object 
    OleDbCommand myCommand = new OleDbCommand(queryStr, myConn);
    // Open the connection 
    myCommand.Connection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();

}

Solution

  • Password is a reserved word. Enclose that field name in square brackets to avoid confusing the db engine.

    string queryStr = "Insert into Users (Username, [Password], Firstname, Surname, Email) values ('" + fname + "','" + sname + "','" + uid + "','" + password + "','" + email + "')";