Update en gridview
Publicado por Marc (8 intervenciones) el 05/12/2008 12:10:15
Hola!!!estoy trabajando con una tabla en las que se pueden añadir o borrar columnas cuando el usuario quiera, al ponerla en el modo Edit y modificar datos no me hace nada en la tabla, les muestro el código:
// aqui obtengo todos los campos que se han modificado
Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView1.RowUpdated
Try
Dim setUpdate As String = " "
Dim key As Object = Nothing
For Each key In e.NewValues.Keys
If Not IsNothing(e.NewValues(key)) Then
setUpdate += key & " = " & e.NewValues(key) & ","
End If
Next
setUpdate = setUpdate.Substring(0, Len(setUpdate) - 1)
Dim sql As String = "UPDATE CLIENTES set " & setUpdate.ToString
UpdateCliente(sql)
e.ExceptionHandled = True
Catch ex As Exception
Label9.Text = e.Exception.Message
End Try
End Sub
// en updateCliente hago la actualización
Sub UpdateCliente(ByVal sql As String)
Dim e As System.Web.UI.WebControls.GridViewUpdatedEventArgs
Dim conexion As OleDbConnection
Dim comando As OleDbCommand
Dim adaptador As OleDbDataAdapter
Dim ds As DataSet = New DataSet
conexion = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Software.mdb")
Try
conexion.Open()
comando = New System.Data.OleDb.OleDbCommand("SELECT * FROM clientes ")
comando.Connection = conexion
adaptador = New OleDbDataAdapter()
adaptador.SelectCommand = comando
adaptador.Fill(ds)
Dim comandoActualizar As OleDbCommand
comandoActualizar = New OleDbCommand("update clientes set nombre=@valNombre Where N_CLIENTE=" & Request.QueryString("id"))
comandoActualizar.Connection = conexion
adaptador.UpdateCommand = comandoActualizar
adaptador.Update(ds)
e.ExceptionHandled = True
Catch ex As Exception
Label9.Text = Err.Description
Finally
If conexion.State = ConnectionState.Open Then
conexion.Close()
End If
End Try
End Sub
En los dos uso el manejador de errores para que no me salte ningún error y pueda continuar
Bueno si alguien se le ocurre alguna idea sobre el código o otra manera mas sencilla de hacer todo esto, que seguro que la hay, que me conteste, que le estare muy agradecido!!!!
gracias!!!
// aqui obtengo todos los campos que se han modificado
Protected Sub GridView1_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles GridView1.RowUpdated
Try
Dim setUpdate As String = " "
Dim key As Object = Nothing
For Each key In e.NewValues.Keys
If Not IsNothing(e.NewValues(key)) Then
setUpdate += key & " = " & e.NewValues(key) & ","
End If
Next
setUpdate = setUpdate.Substring(0, Len(setUpdate) - 1)
Dim sql As String = "UPDATE CLIENTES set " & setUpdate.ToString
UpdateCliente(sql)
e.ExceptionHandled = True
Catch ex As Exception
Label9.Text = e.Exception.Message
End Try
End Sub
// en updateCliente hago la actualización
Sub UpdateCliente(ByVal sql As String)
Dim e As System.Web.UI.WebControls.GridViewUpdatedEventArgs
Dim conexion As OleDbConnection
Dim comando As OleDbCommand
Dim adaptador As OleDbDataAdapter
Dim ds As DataSet = New DataSet
conexion = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:Software.mdb")
Try
conexion.Open()
comando = New System.Data.OleDb.OleDbCommand("SELECT * FROM clientes ")
comando.Connection = conexion
adaptador = New OleDbDataAdapter()
adaptador.SelectCommand = comando
adaptador.Fill(ds)
Dim comandoActualizar As OleDbCommand
comandoActualizar = New OleDbCommand("update clientes set nombre=@valNombre Where N_CLIENTE=" & Request.QueryString("id"))
comandoActualizar.Connection = conexion
adaptador.UpdateCommand = comandoActualizar
adaptador.Update(ds)
e.ExceptionHandled = True
Catch ex As Exception
Label9.Text = Err.Description
Finally
If conexion.State = ConnectionState.Open Then
conexion.Close()
End If
End Try
End Sub
En los dos uso el manejador de errores para que no me salte ningún error y pueda continuar
Bueno si alguien se le ocurre alguna idea sobre el código o otra manera mas sencilla de hacer todo esto, que seguro que la hay, que me conteste, que le estare muy agradecido!!!!
gracias!!!
Valora esta pregunta


0