2

I am trying to automatically insert numbers in my MS Access db .

Currently i have a db that looks like:

| id | username | password | name | email |

where id is auto incremented.

And I have this code:

cmd.Parameters.AddWithValue("@userName", tbUname.Text.Trim());
cmd.Parameters.AddWithValue("@password", tbPass.Text.Trim());
cmd.Parameters.AddWithValue("@Name", tbName.Text.Trim());
cmd.Parameters.AddWithValue("@Email", tbEmail.Text.Trim());
string sql = "insert into users " +
            "values('',@userName, @password, @Name, @Email)";

I have tried doing this:

INSERT INTO users (username,password,name,email) VALUES (@userName,@password, @Name, @Email)

the error i get when u run this query is :

System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at System.Data.OleDb.OleDbCommand.ExecuteNonQuery() at Default2.btsignup_Click(Object sender, EventArgs e)

But it doesn't work. I need it so that whenever a new user registers, a number is assigned to them.

Can some one help me out?

8
  • 2
    Do not store passwords in plain text. Commented Apr 25, 2017 at 15:30
  • What error do you get? Commented Apr 25, 2017 at 15:30
  • its just an example please answer the question being asked :) Commented Apr 25, 2017 at 15:30
  • System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandl ...thats the error when i try using the second commad Commented Apr 25, 2017 at 15:32
  • Can you edit your question to show the full stack trace. The above comment is cut off Commented Apr 25, 2017 at 15:37

1 Answer 1

1

I'm gonna take a punt that password is a reserved field name within MS Access

try

cmd.Parameters.AddWithValue("@userName", tbUname.Text.Trim());
cmd.Parameters.AddWithValue("@password", tbPass.Text.Trim());
cmd.Parameters.AddWithValue("@Name", tbName.Text.Trim());
cmd.Parameters.AddWithValue("@Email", tbEmail.Text.Trim());
string sql = "INSERT INTO users (username,[password],name,email) VALUES (@userName,@password, @Name, @Email)";

note the [ ] around password

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

3 Comments

@MikeMcCaughan see his question - he did actually try specifying the column names: INSERT INTO users (username,password,name,email) VALUES (@userName,@password, @Name, @Email) - problem was the 'naked' password column name
Why not [ ] for every field?
Why not indeed. Go on, splash out, stick [ ] around every column :)

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.