Hi, I have an update form where user can update any record in the database by filling in new values in textboxes. I have succeeded in updating ONE FIELD already, however, i want to be able to update many fields in just one update statement. I know the statement should be "UPDATE TABLENAME SET FIELD1=value1, FIELD2= Value2... WHERE ...." but it just won't work. can anyone help me? below is my code (for updating a single field):
Public Function UpdateData(ByVal TableName As String, ByVal Field As String, ByVal NewValue As String, ByVal WhereField As String, ByVal WhereValue As String)
Dim Str As String
Dim ObjConnection As New OleDb.OleDbConnection
ObjConnection = MyConnection()
Str = "UPDATE " & TableName & " SET " & Field & "=" & NewValue & " WHERE " & WhereField & "=" & WhereValue
MsgBox(Str)
Dim ObjCommand As New OleDb.OleDbCommand
ObjCommand.Connection = ObjConnection
ObjCommand.CommandType = CommandType.Text
ObjCommand.CommandText = Str
Try
ObjCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Private Sub cmdSubmit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
Dim LastName As String = "'" & txtLastName1.Text & "'"
Dim FirstName As String = "'" & txtFirstName1.Text & "'"
Dim MiddleName As String = "'" & txtMiddleName1.Text & "'"
Try
Processor.UpdateData("Employee", "LastName", LastName, "UserID", Global.UserName) Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Function UpdateData(ByVal TableName As String, ByVal Field As String, ByVal NewValue As String, ByVal WhereField As String, ByVal WhereValue As String)
Dim Str As String
Dim ObjConnection As New OleDb.OleDbConnection
ObjConnection = MyConnection()
Str = "UPDATE " & TableName & " SET " & Field & "=" & NewValue & " WHERE " & WhereField & "=" & WhereValue
MsgBox(Str)
Dim ObjCommand As New OleDb.OleDbCommand
ObjCommand.Connection = ObjConnection
ObjCommand.CommandType = CommandType.Text
ObjCommand.CommandText = Str
Try
ObjCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
Private Sub cmdSubmit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
Dim LastName As String = "'" & txtLastName1.Text & "'"
Dim FirstName As String = "'" & txtFirstName1.Text & "'"
Dim MiddleName As String = "'" & txtMiddleName1.Text & "'"
Try
Processor.UpdateData("Employee", "LastName", LastName, "UserID", Global.UserName) Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub