delete command doesn't work as I want

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
' --------------------------------------------------------------------------
 
R

Rick Spiewak

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
' --------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top