0

Pls, am having an error code when inserting data into Access database. It keeps saying there's sytanx error in my INSERT INTO statement. Can any one help me to solve this. Here is the code

try {  
            OleDbConnection connection = new OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\DELL\Documents\EmployeesData.accdb;  
            Persist Security Info = false;");  
            connection.Open();  
            OleDbCommand cmd = new OleDbCommand("insert into EmployeeInfo (UserName, Password) values('" + UserText.Text + "', '" + PassText.Text + "')", connection);  
            cmd.ExecuteNonQuery();  
            MessageBox.Show("Inserted");  
        }  
        catch (Exception ex)  
        {  
            MessageBox.Show("Failed" + ex.ToString());  
        }  
1
  • You could do worse than asking Little Bobby Tables for some advice... Commented Jul 11, 2016 at 9:59

1 Answer 1

3

Password is a keyword in MSACCESS so u need to enclosed in [] bracket

 OleDbCommand cmd = new OleDbCommand("insert into EmployeeInfo ([UserName], [Password])
 values('" + UserText.Text + "', '" + PassText.Text + "')", connection);  

Note: always use parameterized queries to avoid SQL Injection

Sign up to request clarification or add additional context in comments.

2 Comments

Not working the error code says "Index or primary key cannot contain a Null value"
@OyetokeTobiEmmanuel in your table there is a primary key and you are not passing value for it.. thats why its giving this erro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.