开发者

updating access with values in datagrid

开发者 https://www.devze.com 2023-02-21 04:16 出处:网络
Not able to update the table in access using the below function Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click

Not able to update the table in access using the below function

Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click


Dim cn As New OleDbConnection(Con)
        Dim objUpdateCommand As New OleDbCommand
        Dim objDataAdapter As New OleDbDataAdapter
        cn.Open()
        Const SQLExpression As String = "UPDATE product SET Value = @pValue where owner = @powner and Item = @pItem;"
        objUpdateCommand = New 开发者_开发知识库OleDbCommand(SQLExpression, cn)

        With objUpdateCommand
            .Parameters.Add("@powner", OleDbType.VarChar, 10, "owner")
            .Parameters.Add("@pItem", OleDbType.VarChar, 8, "Item")
            .Parameters.Add("@pValue", OleDbType.VarChar, 10, "Value")
        End With

        objDataAdapter.UpdateCommand = objUpdateCommand

        MsgBox("Table Updated")

End Sub


Setting the updateCommand property doesn't execute the command. You need to call the update method of the table adapter.

See more info here


You don't have to have an adapter to just run an update statement with or without parameters. Replace your code (objDataAdapter.UpdateCommand = objUpdateCommand) with this:

Dim UpdateResultValue as Integer

UpdateResultValue = objUpdateCommand.ExecuteNonQuery

If UpdateResultValue = 0 Then
   msgbox "Table Update Failed"
Else
   msgbox "Table Updated"
End if

If you a zero is returned, it didn't work (UpdateResultValue = 0). ExecuteNonQuery is used when you are not returning values from the database tables(probably over-simplified).


Your parameters need to be in same order they are used in your query.

0

精彩评论

暂无评论...
验证码 换一张
取 消