Foros del Web » Programación para mayores de 30 ;) » .NET »

[SOLUCIONADO] como actulizar bd desde un datagrivie no enlazado a una bd?

Estas en el tema de como actulizar bd desde un datagrivie no enlazado a una bd? en el foro de .NET en Foros del Web. Saludos a todos mi duda es la siguiente tengo un datagridview que no esta enlazado a ninguna tabla de la bd pero si qiero que ...
  #1 (permalink)  
Antiguo 27/08/2013, 14:01
 
Fecha de Ingreso: junio-2013
Mensajes: 18
Antigüedad: 10 años, 10 meses
Puntos: 0
como actulizar bd desde un datagrivie no enlazado a una bd?

Saludos a todos mi duda es la siguiente

tengo un datagridview que no esta enlazado a ninguna tabla de la bd pero si qiero que el contenido que yo agrego manuelmente al datagridview,actualice campos ya existente en una de las tablas de la BD

LES EXPLICO COMO REALIZO EL PRECESO
1- BUSCO EL PRODUCTO SI LO ENCUNETRA RELLENA TODOS LOS CAMPOS TEXTBOX
2-HAGO LOS CAMBIOS QUE VOY A REALIZAR EN LOS TEXTBOX
3-AGREGO ESE PRODUCTO AL DATAGRIDVIEW CON SUS CAMBIO QUE REALICE ANTERIORMENTE CON UN BOTON LLAMADO "AGREGAR"
LOS CAMPOS QUE SE AGREGAN AL DATAGRIDVIEW SON LOS SIGUIENTES
CODPRODUCTO PCOMPRA PVENTA ENTRADA EXISTENCIA
PERO SOLAMENTO PCOMPRA PVENTA ENTRADA EXISTENCIA VOY ACTUALIZAR EN MI TABLA PRODUCTO

4-Y VUELVO A REPETIR EL PROCESO AGREGANDO OTRO PRODCUTO AL DATAGRIDVIEW

AHORA LES MUESTRO EL CODIGO EN VBNET COMO LO TENGO Q NO ME FUNCIONA

RECUERDEN QUE TODO ESTO LO REALIZO EN VBNET

TAMBIEN QUE EN EL DATAGRID PUEDN AVER MAS DE UN PRODUCTO
Código PHP:
    Public Sub update_con_txtfindnomprod()
        
Dim query As String "update producto set pc=@pc, pv=@pv, entrada=@entrada, existencia=@existencia where cod_producto=@codprod "
        
Dim lsqlcomand As New SqlCommand(querycnx)

        If 
Me.txtnfactura.Text <> "" And Me.txtcodprod.Text <> "" And Me.txtpc.Text <> "" And Me.txtpv.Text <> "" And Me.txtentrada.Text <> "" And Me.txtexist.Text <> "" Then

            cnx
.Open()
            Try
                For 
Each row As DataGridViewRow In DataGridView1.Rows
                    lsqlcomand
.Parameters.Clear()
                    
lsqlcomand.Parameters.AddWithValue("@pc"Convert.ToDouble(row.Cells("pc").Value))
                    
lsqlcomand.Parameters.AddWithValue("@pv"Convert.ToDouble(row.Cells("pv").Value))
                    
lsqlcomand.Parameters.AddWithValue("@entrada"Convert.ToString(row.Cells("entrada").Value))
                    
lsqlcomand.Parameters.AddWithValue("@existencia"Convert.ToString(row.Cells("existencia").Value))
                
Next
                lsqlcomand
.ExecuteNonQuery()
                
cnx.Close()
                
MessageBox.Show("Actualizacion Realizada Correctamente"".::::."MessageBoxButtons.OK)
            Catch 
ex As Exception
                MsgBox
(ex.Message)
            
End Try

        Else
            
MessageBox.Show("Debe de llenar todos los campos"".::::."MessageBoxButtons.OK)
        
End If
    
End Sub 
YA PUEDO ACTUALIZAR HE ARREGLADO EL CODIGO PERO SE ACTUALIZAN TODO LOS PRODUCTO CON LA MISMA INFORMACION
AUQI ESTA EL CODIGO
Código PHP:
Public Sub update_con_txtfindnomprod()
        
Dim query As String "update producto set pc=@pc, pv=@pv, entrada=@entrada, existencia=@existencia"
        
Dim lsqlcomand As New SqlCommand(querycnx)

        
cnx.Open()
        Try
            
With lsqlcomand
                
For Each row As DataGridViewRow In DataGridView1.Rows
                    lsqlcomand
.Parameters.Clear()
                    
lsqlcomand.Parameters.AddWithValue("@pc"CStr(row.Cells("pc").Value))
                    
lsqlcomand.Parameters.AddWithValue("@pv"CStr(row.Cells("pv").Value))
                    
lsqlcomand.Parameters.AddWithValue("@entrada"CStr(row.Cells("entrada").Value))
                    
lsqlcomand.Parameters.AddWithValue("@existencia"CStr(row.Cells("exist").Value))
                
Next
            End With
            lsqlcomand
.ExecuteNonQuery()
            
cnx.Close()
            
MessageBox.Show("Actualizacion Realizada Correctamente"".::::."MessageBoxButtons.OK)
        Catch 
ex As Exception
            MsgBox
(ex.Message)
        
End Try
  
End Sub 

Última edición por lordbeliar2010; 27/08/2013 a las 17:29
  #2 (permalink)  
Antiguo 28/08/2013, 12:30
 
Fecha de Ingreso: junio-2013
Mensajes: 18
Antigüedad: 10 años, 10 meses
Puntos: 0
Respuesta: como actulizar bd desde un datagrivie no enlazado a una bd?

OK LES MUESTRO QUE HE LOGRADO AVANZAR ASTA EL MOMENTO .

COMO LES ABIA DICHO ANTERIROMENTE LO QUE DESEO ACER ES LO SIGUIENTE TODO LO QE ESTA EN EL DATAGRIDVIEW1 Y QUE NO ESTA ENLAZADO A NINGUNA BASE DE DATOS Y QUE CON UN BOTON VOY AGREGANDO LOS DATOS EN EL DATAGRIDVIEW1, CON ESOS DATOS QIERO ACTUALIZAR UNA TABLA DE MI BASE DE DATOS PERO SOLO AQUELLOS Q TENGAN EL MISMO CODIGO DE PRODUCTO.

POR ELMOMENTO ESTOY REALIZANDO LO SIGUIENTE AGREGO UNICAMENTEO 2 PRODUCTO AL DATAGRIDVIEW1 Y AL HACER CLIC EN EL BOTON COMPARAR ME COMPARA EL 1ER CODIGO DE PRODUCTO QUE ESTA EN EL DATAGRIDVIEW1 CON EL QUE ESTA EN LA BASE DE DATOS Y EL PROBLEMA ES EL SIGIENTE QUE UNICAMENTE SOLO COMPARA EL 1ER PRODUCTO DEL DATAGRIDVIEW1 NO AVANZA AL SEGUNTO PARA ESTE SER COMPARADO CON LOS Q ESTAN EN LA BASE DE DATOS

LES ABIA DICHO Q TENGO 2 DATAGRIDVIEW AL MONETO DE HACER CLIC EN EL BOTON COMPARAR MUESTRA EL CODIGO DEL PRODUCTO QUE ES IGUAL EN EL DATAGRIDVIEW1 CON EL DE LA BASE DE DATO Y ESTE CODIGO LO MUESTRA EN EL DATAGRIDVIEW2
AQUI LES DEJO EL CODIGO TALVEZ DESDE AQUI ME BRINDA UNA AYUDA
TODO ESTO ES CODIGO VBNET





Código PHP:
Public Sub actualizar_tabla_producto()
        
Dim codigossqlbuscar As String
        Dim i 
As Integer 0
        codigos 
DataGridView1.CurrentRow.Cells(1).Value
        
Try
            
' Establecemos la conexión con la base' 
            
Using cnn As New SqlConnection("Data Source=BT-PC\SQLEXPRESS;Initial Catalog=ferr2013;Integrated Security=True")
                
sqlbuscar = ("SELECT cod_producto FROM producto WHERE cod_producto ='" codigos "'")
                
' Creamos el adaptador de datos'
                
Dim da As New SqlDataAdapter(sqlbuscarcnn)
                
' Creamos un nuevo objeto DataTable'
                
Dim dt As New DataTable("producto")
                
' Rellenamos el objeto DataTable'
                
da.Fill(dt)

                For 
Each Dt_Row As DataGridViewRow In DataGridView1.Rows
                    
If Dt_Row.Cells("codprod").Value.ToString dt.Rows(i)("cod_producto").ToString Then
                        DataGridView2
.DataSource dt
                    End 
If   
                
Next
            End Using
        
Catch ex As Exception
                      MessageBox
.Show(ex.Message)   'Se ha producido un error'
        
End Try
    
End Sub 
  #3 (permalink)  
Antiguo 29/08/2013, 14:36
 
Fecha de Ingreso: junio-2013
Mensajes: 18
Antigüedad: 10 años, 10 meses
Puntos: 0
Respuesta: como actulizar bd desde un datagrivie no enlazado a una bd?

JDPAIZ DESDE MANAGUA, NICARAGUA PARA EL MUNDO

Gracias jose luis por tu pronta respuesta me a ayudado de mucho y leindo en otros foros solo recopile la informacion y logre acerlo aquiles les dejo el codigo para que se ayuden.
TODO ES CODIGO VBNET

Código PHP:
Private Sub cmdsave_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles cmdsave.Click 
        
Try 
            
Using loConexion As New SqlConnection(conexion
                
Dim lsQuery As String String.Empty 
                
loConexion.Open()   ' abrir conexión 
                For Each loFila As DataGridViewRow In Me.DataGridView1.Rows 
                    If lF_bExisteCodigo(loFila.Cells("codprod").Value.ToString()) = True Then 
                        '
Modificar registro 
lsQuery 
"UPDATE producto SET pc=@pc, pv=@pv, entrada=@entrada, existencia=@existencia Where cod_producto=@cod_producto"
                        ' crear comando'                        
                        
Dim loComando As New SqlCommand(lsQueryloConexion
                        
'añadir parametros al comando'  
                        
loComando.Parameters.Clear() 
                        
loComando.Parameters.Add(New SqlParameter("@pc"CStr(loFila.Cells("pc").Value))) 
                        
loComando.Parameters.Add(New SqlParameter("@pv"CStr(loFila.Cells("pV").Value))) 
                        
loComando.Parameters.Add(New SqlParameter("@entrada"CStr(loFila.Cells("entrada").Value))) 
                        
loComando.Parameters.Add(New SqlParameter("@existencia"CStr(loFila.Cells("exist").Value))) 
                        
loComando.Parameters.Add(New SqlParameter("@cod_producto"loFila.Cells("codprod").Value)) 
                        
loComando.ExecuteNonQuery() 
                    
End If 
                
Next  
            End Using 
        
Catch Exp As Exception 
            MessageBox
.Show(Exp.Message"Button2_Click"MessageBoxButtons.OKMessageBoxIcon.Information
        
End Try 
    
End Sub 
TODO ES CODIGO VBNET

Código PHP:
 Private Function lF_bExisteCodigo(ByVal cod_producto As String) As Boolean 
        Dim lbRetorno 
As Boolean False 
        Dim lsQuery 
As String 
        
Try 
            
Using loConexion As New SqlConnection(conexion
                
lsQuery "Select Count(*) From producto Where cod_producto=@cod_producto" 
                
Using loComando As New SqlCommand(lsQueryloConexion
                    
añadir parametros al comando 
                    loComando
.Parameters.Add(New SqlParameter("@cod_producto"cod_producto)) 
                    
loConexion.Open() 
                    
Dim liResultado As Integer CInt(loComando.ExecuteScalar()) 
                    If 
liResultado 0 Then 
                        lbRetorno 
True 
                    End 
If 
                
End Using 
            End Using 
        
Catch Exp As SqlException 
            MessageBox
.Show(Exp.Message"lF_bExisteCodigo"MessageBoxButtons.OKMessageBoxIcon.Information
        Catch 
Exp As Exception 
            MessageBox
.Show(Exp.Message"lF_bExisteCodigo"MessageBoxButtons.OKMessageBoxIcon.Information
        
End Try 
        Return 
lbRetorno 
    End 
Function 

Etiquetas: bd
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 09:35.