i’m trying to delete a row in the database with GetDeleteCommand() and DataAdapter.Update().
with GetInsertCommand() and GetUpdateCommand() the all transactions works, but with GetDeleteCommand() fails, i mean, “run” onto this instruction but the data still remains in the table.
here is my code:
Public Function delete(ByVal tabla As String, ByVal arg As VariantType) As Boolean
Dim ok As Boolean = False
Dim dataSet As DataSet = New DataSet(tabla)
Dim dataRow As DataRow
Dim dataTable As DataTable
Me.sqlComm.Connection = Me.sqlConn
Me.sqlComm.CommandText = "Select * from " & tabla
Try
Me.sqlDA.SelectCommand = Me.sqlComm
'llenar el dataset con los datos de la consulta
Me.sqlDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
Me.sqlDA.Fill(dataSet, tabla)
'nuevo commandbuilder
Me.sqlCB = New SqlCommandBuilder(Me.sqlDA)
'crear el buffer de la tabla
dataTable = dataSet.Tables(tabla)
'buscar el valor en la tabla
dataRow = dataTable.Rows.Find(arg)
'obtener el delete command
Me.sqlDA.DeleteCommand = Me.sqlCB.GetDeleteCommand(True)
'actualizar los registros.
'se supone q con el DeleteCommand deberia borrar en este punto el dato
Me.sqlDA.Update(dataSet, tabla)
ok = True
Catch ex As Exception
Console.WriteLine(ex.StackTrace.ToString() & vbCrLf & ex.Message())
Me.err = ex.StackTrace.ToString() & vbCrLf & ex.Message()
End Try
Me.sqlConn.Close()
Return ok
End Function
as said before, the all code run without errors, but the data found and “deleted” from my code still remain in the table.
what’s wrong? or how to do a correct command or how to do this works?
thanks
You are not actually deleting the row. You find it, but don’t do anything with it after it is found. If you call dataRow.Delete() before your sqlData.Update statement, the row should be deleted.