Ir al contenido


Foto

como actualizar tabla de bd con datagridview no enlazada a bd


  • Por favor identifícate para responder
1 respuesta en este tema

#1 novato

novato

    Member

  • Miembros
  • PipPip
  • 35 mensajes

Escrito 29 agosto 2013 - 01:33

DESEO HACER LO SIGUIENTE TODO TENGO UN  DATAGRIDVIEW Y QUE CON UN BOTON VOY LO VOY LLENANDO,CON ESOS DATOS QIERO ACTUALIZAR UNA TABLA DE MI BASE DE DATOS PERO SOLO AQUELLOS Q TENGAN EL MISMO CODIGO DE PRODUCTO QUE ESTAN EN EL DATAGRIDVIEW

HACE LA ACTUALIZACION BIEN TODO LOS DATOS DE LA TABLA DE LA BD PERO AUN ME SIGUE MONSTRONADO EL SIGIENTE MENSAGE AL MOMENTO DE HACER CLICK EN EL BOTON ESTS ES EL MENSAJE


"REFERENCIA  A OBJETO NO ESTABLECIDA COMO INSTANCIA DE UN OBJETO "
ASI TENGO EL CODIGO

EL CODIGO ES VBNET


Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
        Try
            Dim MiTransaccion As SqlTransaction
            Dim lsQuery As String = String.Empty
            Using loConexion As New SqlConnection(conexion)
                loConexion.Open()  ' abrir conexión
                'Iniciamos una transaccion, hay que agregar referencia a System.Transactions
                Using MiTransaccion
                    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'
                            Using loComando As New SqlCommand(lsQuery, loConexion)
                                'añadir parametros al comando
  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 Using
                        End If
                    Next
                    MiTransaccion.Dispose()
                End Using
            End Using
        Catch Exp As SqlException
            MessageBox.Show(Exp.Message, "Button1_Click", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch Exp As Exception
            MessageBox.Show(Exp.Message, "Button1_Click", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
    '*************************************************************************************************
    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(lsQuery, loConexion)
                    '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.OK, MessageBoxIcon.Information)
        Catch Exp As Exception
            MessageBox.Show(Exp.Message, "lF_bExisteCodigo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        Return lbRetorno
    End Function

  • 0

#2 novato

novato

    Member

  • Miembros
  • PipPip
  • 35 mensajes

Escrito 29 agosto 2013 - 08:20

jdpaiz desde Managua,Nicaragua para el mundo

LO SOLUCIONES DE ESTA MANERA

TODO ES CODIGO VBNET

Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 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(lsQuery, loConexion)
                        '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
                loConexion.Close()
            End Using
Catch Exp As Exception
            MessageBox.Show(Exp.Message, "Button2_Click", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub



  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(lsQuery, loConexion)
                    '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.OK, MessageBoxIcon.Information)
  Catch Exp As Exception
MessageBox.Show(Exp.Message, "lF_bExisteCodigo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
        Return lbRetorno
    End Function

  • 0




IP.Board spam blocked by CleanTalk.