Syntax error. Im not really familiar in using parameter so i dont know whats wrong with code .
i used ms access.

Private Sub btnupdate_Click(sender As System.Object, e As System.EventArgs) Handles btnupdate.Click

        Dim name As String = txtfname.Text
        Dim pos As String = txtpos.Text
        Dim address As String = txtadd.Text
        Dim contact As String = mtnum.Text
        Dim username As String = txtusername.Text
        Dim id As String = txtid.Text

        Dim sql As String = "UPDATE tbluser SET " & _
                                     "name ='" & _
                                      "pos = '" & _
                                      "address ='" & _
                                      "contact = '" & _
                                       "username ='" & _
                                      "WHERE ID = '"
        cmd = New OleDbCommand(sql.ToString, con)

        cmd.Parameters.AddWithValue("@name", name)
        cmd.Parameters.AddWithValue("@pos", pos)
        cmd.Parameters.AddWithValue("@address", address)
        cmd.Parameters.AddWithValue("@contact", contact)
        cmd.Parameters.AddWithValue("@username", username)
        cmd.Parameters.AddWithValue("@ID", id)
        cmd.ExecuteNonQuery()

        MsgBox("save")


    End Sub

Recommended Answers

All 6 Replies

Your codification is quite right, except SQL Statement. It should be

 Dim sql As String = "UPDATE tbluser SET " & _
                                     "name =?" & _
                                      "pos =?" & _
                                      "address =?" & _
                                      "contact =?" & _
                                       "username =?" & _
                                      "WHERE ID =?"

Here is a tutorial, please read it. Hope it can help you.

@Shark_1,error

Sorry, there should be a comma.
And also no need to declare variables. Assign the Parameter values directly from TextBoxes.

Private Sub btnupdate_Click(sender As System.Object, e As System.EventArgs) Handles btnupdate.Click

        Dim sql As String = "UPDATE tbluser SET " & _
                                     "name =?, " & _
                                      "pos =?, " & _
                                      "address =?, " & _
                                      "contact =?, " & _
                                       "username =?, " & _
                                      "WHERE ID =?"

        cmd = New OleDbCommand(sql, con)
        cmd.Parameters.AddWithValue("@name", txtfname.Text)
        cmd.Parameters.AddWithValue("@pos", txtpos.Text)
        cmd.Parameters.AddWithValue("@address", txtadd.Text)
        cmd.Parameters.AddWithValue("@contact", mtnum.Text)
        cmd.Parameters.AddWithValue("@username", txtusername.Text)
        cmd.Parameters.AddWithValue("@ID", txtid.Text)
        cmd.ExecuteNonQuery()
        MsgBox("save")
    End Sub

ok,i got it, thank you for helping me again, :D

@Shark 1, hi again, i tried the code but it still syntax error, i replace the tbluser in database and create again, but its still syntax error, what do you think is wrong with my database/code?

I replace "&" with "+" and it works now, :)

Dim sql As String = "UPDATE tbluser SET name=?, pos=?, address=?, contact=?, username=?" +
            "WHERE ID =?"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.