How can I store data from a datagridview to a MySql database?

1

I have a datagridview that is filled with student data according to a search made through a combobox preloaded with workshops in which students register. My question is: How can I insert only specific datagridview fields into the database?

I append the code that I have in the save button and it effectively stores when it is pressed for the first time, then closes the window and when I want to add new records executing that action by means of the same button, the registers are registered as' NULL '

Note: This only happens every two times (one record is sent correctly, the next one is sent as NULL, the next one correctly, the next one as NULL)

Private Sub btnGuardar_Click(sender As Object, e As EventArgs) Handles btnGuardar.Click

    Try
        cmCon = New MySqlCommand
        adCon.Open()
        cmCon.Connection = adCon
        ''Dim query As String = "Insert into asistencia (fecha, tipoasistencia, idregistro) values(@fecha, @tipoasistencia, @idregistro)"

        cmCon.CommandText = "Call spInsAsistencia(@fecha, @tipoasistencia, @idregistro)"

        For Each row As DataGridViewRow In dgvAsistencia.Rows
            cmCon.Parameters.Clear()
            cmCon.Parameters.AddWithValue("@fecha", FormatoFecha(dtpFecha.Value))
            cmCon.Parameters.AddWithValue("@tipoasistencia", CStr(row.Cells(0).Value))
            cmCon.Parameters.AddWithValue("@idregistro", CStr(row.Cells(1).Value))

            cmCon.ExecuteNonQuery()


        Next

        MsgBox("Guardado correctamente", MsgBoxStyle.Information, "Información")
        frmAsistencias.ActualizarGrid()



    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        adCon.Close()
        Me.Close()

    End Try

End Sub
    
asked by Carlos 25.05.2018 в 05:31
source

0 answers