Good afternoon! I'm having some difficulties in submitting the combobox data in the listview due to an error that appears and I can not resolve.
Good afternoon! I'm having some difficulties in submitting the combobox data in the listview due to an error that appears and I can not resolve.
I'm adding another answer to not pollute my current solution that would be the most correct.
Your output is to create a class that I will call ComboBoxItem
and add the values using it, rather than adding it to the Properties.
Class ComboBoxItem
Public Property Text As String ' sua descrição
Public Property Value As Integer ' seu código
Sub New(ByVal text As String, value As Integer)
Me.Text = text
Me.Value = value
End Sub
Public Overrides Function ToString() As String
Return Me.Text
End Function
End Class
And in your CodeBehind you will be able to combobox
popular like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim computadores As List(Of ComboBoxItem) = New List(Of ComboBoxItem)
computadores.Add(New ComboBoxItem("D11", 1))
computadores.Add(New ComboBoxItem("D12", 2))
computadores.Add(New ComboBoxItem("D13", 3))
pcTB.DataSource = computadores
End Sub
And to recover the value just make it look like my other solution:
.Add("@codComp", SqlDbType.Int).Value = CType(pcTB.SelectedItem, ComboBoxItem).Value
Based on the comments I believe your problem is not in the structure but in the way that you are retrieving the value of combobox
, you only have descrição
in combobox
and not% / p>
I asked about how you are populating id
to give you a more accurate solution, if you are just filling in the description or you are passing the object of type pcTB
.
Computador
At the time you load your data into Computador
.
' Verifica o nome correto aqui utilizei 'Form1'
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'seu código se ja tiver
' caso você não tenha esse if ainda, é bom ter
' ele verifica se foi um PostBack, exemplo se clicou no botão é PostBack
' se você acabou de carregar o Form então NÃO é PostBack
If Not Page.IsPostBack Then
CarregaComputadores()
End If
End Sub
Private Sub CarregaComputadores()
' aqui você prepara a conexão com seu banco
Dim objconnect2 As SqlConnection = 'sua conexão
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter()
Dim ds As New DataSet()
Dim sql As String = "select codComp, descricaoComp FROM Computadores"
Try
connection.Open()
command = New SqlCommand(sql, objconnect2)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
connection.Close()
pcTB.DataSource = ds.Tables(0)
'Especificar qual coluna exibe e qual valor retorna
pcTB.DisplayMember = "descricaoComp" 'Campo que exibe para usuário
pcTB.ValueMember = "codComp" 'Valor para esse texto
Catch ex As Exception
MessageBox.Show("Erro ao carregar os computadores! ")
End Try
End Sub
And then to recover these values you do so
.Add("@codComp", SqlDbType.Int).Value = ctype(pcTB.SelectedItem, Computador).codComp
Pay attention to combobox
and SqlDbType.Int
to retrieve the value.