delete command doesn't work as I want

  • Thread starter Thread starter xan
  • Start date Start date
X

xan

Here is want I and writting. It works, but it delete another record not the
only where I click the command delete button.

What I'm doing wrong?

' -------------------------------------------------------------------------
Sub dgDatos_Delete(obj As object, e As DataGridCommandEventArgs)
Dim strSQL As String = "DELETE FROM datos " & _
"WHERE numero = " & e.Item.ItemIndex & ";"

ExecuteStatement(strSQL)

LlenarDatosDeCuadricula()
lblMensaje.Text = lblMensaje.Text & e.Item.cells(0).Text
End Sub
' ----------------------------------------------------------------------
function ExecuteStatement(strSQL)
Dim objCmd As New OleDbCommand(strSQL, Conn)

try
objCmd.Connection.Open()
lblmensaje2.text=""
objCmd.ExecuteNonQuery()
catch ex As Exception
lblMensaje.Text = "consulta:<br>" & strSQL & "<br>Error al
actualizar la base de datos.<br> " & ex.tostring()
End try

objCmd.Connection.Close()
End function
' --------------------------------------------------------------------------
 
Use e.item.datasetindex to find the correct offset into the database. But,
this may not always correspond to the "numero" field. You may want to use
this index to locate the record in your datasource (the row in a dataset,
for example), and use one or more of the fields from that record to
construct your delete command to be sure that you get the correct record.

xan said:
Here is want I and writting. It works, but it delete another record not the
only where I click the command delete button.

What I'm doing wrong?
' -------------------------------------------------------------------------
Sub dgDatos_Delete(obj As object, e As DataGridCommandEventArgs)
Dim strSQL As String = "DELETE FROM datos " & _
"WHERE numero = " & e.Item.ItemIndex & ";"

ExecuteStatement(strSQL)

LlenarDatosDeCuadricula()
lblMensaje.Text = lblMensaje.Text & e.Item.cells(0).Text
End Sub
' ----------------------------------------------------------------------
function ExecuteStatement(strSQL)
Dim objCmd As New OleDbCommand(strSQL, Conn)

try
objCmd.Connection.Open()
lblmensaje2.text=""
objCmd.ExecuteNonQuery()
catch ex As Exception
lblMensaje.Text = "consulta:<br>" & strSQL & "<br>Error al
actualizar la base de datos.<br> " & ex.tostring()
End try

objCmd.Connection.Close()
End function
' --------------------------------------------------------------------------
 
Back
Top