0

Im using VB.net and MS Access 2007 as backend. Im getting an error when im trying to save it. this is my code for Save button:

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Try
            con = New OleDbConnection(cn)
            con.Open()
            Dim cb As String = "INSERT INTO LoginDetails (EmpID, EmpName, Username, Password) VALUES (@d1, @d2, @d3, @d4)"
            'Dim cb As String = " INSERT INTO LoginDetails (EmpID, EmpName, Username, Password) VALUES ('" & cboEmpID.Text & "', '" & txtEmpName.Text & "', '" & txtUsername.Text & "', '" & txtPassword.Text & "')"
            cmd = New OleDbCommand(cb)
            cmd.Connection = con
            cmd.Parameters.Add(New OleDbParameter("@d1", OleDbType.VarChar, 30, "EmpID"))
            cmd.Parameters.Add(New OleDbParameter("@d2", OleDbType.VarChar, 30, "EmpName"))
            cmd.Parameters.Add(New OleDbParameter("@d3", OleDbType.VarChar, 30, "Username"))
            cmd.Parameters.Add(New OleDbParameter("@d4", OleDbType.VarChar, 30, "Password"))
            cmd.Parameters("@d1").Value = cboEmpID.Text
            cmd.Parameters("@d2").Value = txtEmpName.Text
            cmd.Parameters("@d3").Value = txtUsername.Text
            cmd.Parameters("@d4").Value = txtPassword.Text
            cmd.ExecuteNonQuery()
            MessageBox.Show("Registration completed", "Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information)
            cmdSave.Enabled = False
            cboEmpID.Focus()
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
apomene
  • 14,282
  • 9
  • 46
  • 72
  • Can you state the line where you're getting the error, and include the error text / condition? – Andrew Mortimer Jul 01 '15 at 12:49
  • "Password" is a [reserved word in Jet](https://support.microsoft.com/en-us/kb/321266); so you need to escape it: `[Password]`. Also, passwords should never be stored as plain text, you should [hash them](http://stackoverflow.com/a/31150288/1070452) – Ňɏssa Pøngjǣrdenlarp Jul 01 '15 at 13:26

1 Answers1

0

Try more simply:

cmd.Parameters.Add("@d1", OleDbType.VarChar,30).Value = cboEmpID.Text

Rob
  • 3,488
  • 3
  • 32
  • 27