0
strSQL = "INSERT INTO Accounts UserName, Password VALUES ('" & txtUsername.Text & "', '" & txtEncryptedPassword & "');"

When the code is executed and error is thrown, but there is no visible problem that i can see. Help!

4 Answers4

5

The word PASSWORD is reserved in MS-Access.
You need to use square brackets around that name (Or change it to something different)

strSQL = "INSERT INTO Accounts (UserName, [Password]) VALUES (......

Said that, please use a parameterized query to build sql commands.
A string concatenation like yours is easily attacked by hackers using SQL Injection
Also, if the username or password contains a single quote, the resulting sql text built using string concatenation will be invalid.

strSQL = "INSERT INTO Accounts (UserName, [Password]) VALUES (?, ?)"
OleDbCommand cmd = new OleDbCommand(strSQL, connection);
cmd.Parameters.AddWithValue("@p1",txtUsername.Text);
cmd.Parameters.AddWithValue("@p2",txtEncryptedPassword);
cmd.ExecuteNonQuery();
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
1

You forgot parentheses:

strSQL = "INSERT INTO Accounts (UserName, Password) VALUES ('" & txtUsername.Text & "', '" & txtEncryptedPassword & "');"
Alexander
  • 3,129
  • 2
  • 19
  • 33
0

your doing () this mistake and you should must add:

your code:

strSQL = "INSERT INTO Accounts UserName, Password VALUES ('" & txtUsername.Text & "', '" & txtEncryptedPassword & "');"

you should must change code following as:

strSQL = "INSERT INTO Accounts (UserName, Password) VALUES ('" & txtUsername.Text & "', '" & txtEncryptedPassword & "');"

update1:

"INSERT INTO `test`.`users` ( `username`, `password`) " & _
                  "VALUES ('" & txtUsername.Text & "', '" & txtPassword.Text & "');"

update2:

   "INSERT INTO users ( `username`,`password`)VALUES(@txtUsername.Text,@txtPassword.Text);"

"INSERT INTO users (Username,Password)VALUES(?,?);"

note:test means database name you should change your databasename.

jmail
  • 5,944
  • 3
  • 21
  • 35
0

try this code:

 Dim strSQL As String = "INSERT INTO tblDetail VALUES('" & strPersonCode _
         & "','" & strForename & "','" & strSurname & "','" & strDateOfBirth & "'," & strCurrentlyWith & ",'" & strConditions & "')"

Do it like that but change to your names. Declare the values of text boxes as strings and just use those.

jmail
  • 5,944
  • 3
  • 21
  • 35