Problem with using UpdateCommand property of the DataAdapter

Abbasi

Member
Joined
Nov 2, 2005
Messages
17
Location
Pakistan
Programming Experience
3-5
The following procedure does not Update the record(s).
'------------------------------------------------------------------
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim dadapter As OleDbDataAdapter = New OleDbDataAdapter
Dim CmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(dadapter)
Dim strSelect = "SELECT CustomerID, CompanyName FROM Customers"
Dim cmdSelect As OleDbCommand = New OleDbCommand(strSelect, Conxn)
Dim DS As DataSet = New DataSet
dadapter.SelectCommand = cmdSelect
dadapter.Fill(DS, "Customers")
Dim strUpdate = "UPDATE " & strCustomers & " " & _
"SET CustomerID = 'COMID', " & _
"CompanyName = 'Cactus Comidas'" & _
"WHERE CustomerID = 'CACTU'"
Dim cmdUpdate As OleDbCommand = New OleDbCommand(strUpdate, Conxn)
dadapter.UpdateCommand = cmdUpdate
Try
Dim NumRows As Long = dadapter.Update(DS, "Customers")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub
'---------------------------------------------------
For academic purpose, I need a solution based on the Update method of the DataAdapter (Not ExecuteNonQuery of the Command object). Please
suggest a solution.
Regards.
R. A. Abbasi
 
Update is used to commit changes that you have made to your DataTable to the database. You haven't made any changes to the DataTable so the call to Update has nothing to do. If you want to execute an explicit SQL statement like that then you should be calling ExecuteNonQuery. If you have to call Update for your assignment then you need to make the change to the DataTable first. Also, you're using a CommandBuilder and an UpdateCommand. Use one or the other, not both. Your knowledge of ADO.NET is obviously hazy, which is no crime, but I suggest that you do some reading. Try some of the tutorial links in my signature as well as the 101 samples.
 
Back
Top