2
Hello, I am developing an application in Vb and would like to know, how to pass a list created in Listviw control to the database? until the moment I built it:
Private Sub criarLista()
ListView1.Clear()
ListView1.View = View.Details
ListView1.LabelEdit = True
ListView1.AllowColumnReorder = True
ListView1.CheckBoxes = True
ListView1.FullRowSelect = True
ListView1.GridLines = True
ListView1.Sorting = SortOrder.Ascending
If btnInclusao.BackColor = Color.Silver Then
With ListView1
.Columns.Add("Codigo da Coleta", 120, HorizontalAlignment.Left)
.Columns.Add("Observações", 120, HorizontalAlignment.Left)
End With
End If
If btnManutencao.BackColor = Color.Silver Then
With ListView1
.Columns.Add("Codigo da Coleta", 120, HorizontalAlignment.Left)
.Columns.Add("Observações", 120, HorizontalAlignment.Left)
.Columns.Add("Baixa da Coleta", 120, HorizontalAlignment.Left)
End With
End If
End Sub
the code that adds the items inside the list:
Dim newItem As New ListViewItem(CodColeta_txt.Text)
newItem.SubItems.Add(obsColeta_txt.Text)
I tried to do it that way:
Private Sub inserirListadeColeta()
reconect()
For Each item As ListViewItem In ListView1.Items
cmd.CommandText = "INSERT INTO ListaParaColeta (fk_ColetaParaMotorista, ListaParaColeta_CodColeta, ListaParaColeta_osbColeta" _
& "ListaParaColeta_periodo, ListaParaColeta_dataRegistro) VALUES (@fk, @CodColeta, @obsColeta, @periodo, @data) "
Using conn
Using cmd
With cmd
.Connection = conn
.CommandType = CommandType.Text
.Parameters.Add("@fk", MySqlDbType.Int32).Value = codParaMotorista
.Parameters.Add("@CodColeta", MySqlDbType.Int32).Value = item.SubItems(0).Text 'Campo do ListViwer Codigo da Coleta
.Parameters.Add("@obsColeta", MySqlDbType.VarChar).Value = item.SubItems(1).Text ' Campo do ListViwer OBS
.Parameters.Add("@periodo", MySqlDbType.Int32).Value = cmbPeriodo.SelectedIndex
.Parameters.Add("@data", MySqlDbType.Date).Value = Format(DataHoje.Value, "yyyyMMdd")
End With
Try
cmd.ExecuteReader()
Clipboard.Clear()
Clipboard.SetText(strsql)
MsgBox("inserirListadeColeta")
Catch ex As Exception
MsgBox("Erro inserirListadeColeta" & vbCrLf & ex.ToString, MsgBoxStyle.Critical, "Erro")
Clipboard.Clear()
Clipboard.SetText(strsql)
Exit Sub
End Try
End Using
End Using
Next
End Sub
Thanks in advance!