0

This is my save code
There is error when I try to save
Error Message ( Syntax error INSERT INTO statement )

I have checked my database table it's same to my code

The error image in under the code

Public Sub adduser()
    Try
        If conn.state = connectionstate.closed Then
            conn.Open()
        End If

        Dim cmd As New oledbcommand
        cmd.connection = conn
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "INSERT INTO login ([name], email, phone, username, password, role_1, inventory_1, delete_1, update_1, settings_1, backup_1, reports_1, employee_1, loan_1, debt_1) VALUES (@name, @email, @phone, @username, @password, @role_1, @inventory_1, @delete_1, @update_1, @settings_1, @backup_1, @reports_1, @employee_1, @loan_1, @debt_1)"

        cmd.parameters.addwithvalue("@name", textbox5.text.trim)
        cmd.Parameters.AddWithValue("@email", TextBox5.Text.Trim)
        cmd.Parameters.AddWithValue("@phone", TextBox3.Text.Trim)
        cmd.parameters.addwithvalue("@username", textbox1.text.trim)
        cmd.parameters.addwithvalue("@password", textbox2.text.trim)
        cmd.Parameters.AddWithValue("@role_1", ComboBox1.Text.Trim)

        cmd.Parameters.AddWithValue("@inventory_1", CheckBox1.Checked)
        cmd.Parameters.AddWithValue("@delete_1", CheckBox1.Checked)
        cmd.Parameters.AddWithValue("@update_1", CheckBox1.Checked)
        cmd.Parameters.AddWithValue("@settings_1", CheckBox1.Checked)
        cmd.Parameters.AddWithValue("@backup_1", CheckBox1.Checked)
        cmd.Parameters.AddWithValue("@reports_1", CheckBox1.Checked)
        cmd.Parameters.AddWithValue("@employee_1", CheckBox1.Checked)
        cmd.Parameters.AddWithValue("@loan_1", CheckBox1.Checked)
        cmd.Parameters.AddWithValue("@debt_1", CheckBox1.Checked)

        cmd.ExecuteScalar()
        conn.close()

        userregistration.loadaccounts()
        msgbox("Added")

        textbox1.text = ""
        textbox2.text = ""
        textbox3.text = ""
        textbox4.text = ""
        textbox5.text = ""
        combobox1.text = ""

        checkbox1.checked = False
        checkbox2.checked = False
        checkbox3.checked = False
        checkbox4.checked = False
        checkbox5.checked = False
        checkbox6.checked = False
        checkbox7.checked = False
        checkbox8.checked = False
        checkbox9.checked = False
        checkbox10.checked = False

    Catch ex As Exception
        MsgBox("Error !: " + ex.Message)
    End Try
End Sub

this is the error

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • This is a well known situation. Password is a reserved keyword. Write it as [Password] – Steve Oct 25 '20 at 08:44
  • 1
    The code shows also well known bad practices. Do not keep a global connection object. Do not use AddWithValue. To execute the query use ExecuteNonQuery not ExecuteScalar. The latter is used when you expect a single return value from the query – Steve Oct 25 '20 at 08:47
  • 1
    You should check out [Can we stop using AddWithValue() already?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) and stop using `.AddWithValue()` - it can lead to unexpected and surprising results... – marc_s Oct 25 '20 at 08:47
  • 1
    Glad to be of help. In particular look at the second duplicate above with examples that fix the other problems I mentioned in the comment above – Steve Oct 25 '20 at 10:09
  • **NEVER** store password as plain text. – Mary Oct 25 '20 at 18:10

0 Answers0