Combobox and database connection

Asked

Viewed 602 times

2

Good afternoon! I’m having some difficulty submitting the combobox data in listview due to an error that appears and I can not solve.

inserir a descrição da imagem aqui

The mistake you make is this

inserir a descrição da imagem aqui

This here is the code where the error is pointed out(black arrow)

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

This is the mistake

2 answers

0

Based on the comments I believe that your problem is not in the structure but in the way you are recovering the value of combobox, you just own the descrição in the combobox and not the identifier id.

I asked about how you are populating the pcTB to give you a more accurate solution, if you are just filling in the description or are passing the object of type Computador.

Solution using the model Computador

When you upload your data to combobox.

' 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 those values you do so

.Add("@codComp", SqlDbType.Int).Value = ctype(pcTB.SelectedItem, Computador).codComp

Pay attention to the SqlDbType.Int and to the CAST to recover the value.

  • gives error in " as ", appears ')' expected

  • @Forrobodo Wobble my, I used the cast of C#, I have now updated correctly on vb.net :)

  • @Forrobodo Post the code of how you put values in pcTB, how are you assigning value to him? That way I can adjust the answer

0


I am adding another answer to not pollute too much my current solution that would be the most correct.

Your way out is to create a class I’ll call ComboBoxItem and add the values using it instead of adding 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 popular the comboboxthus:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.