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
@Forrobodo Wobble my, I used the cast of
C#
, I have now updated correctly onvb.net
:)– Maicon Carraro
@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– Maicon Carraro