Jump to content


Photo

Combo Box - Vb.net


  • Faça o login para participar
5 replies to this topic

#1 cacfs

cacfs

    Turista

  • Usuários
  • 50 posts
  • Sexo:Não informado

Posted 11/03/2005, 16:11

Estou tentando preenchendo um ComboBox com um DataSet que contem dados de uma BD. Quando tento detectar o valor selecionado, está dando erro. Já tentei de tudo.
Quem puder me ajudar, segue o código abaixo:

    'carrega pesquisas
    Public Sub carrega_pesquisas()
        Dim strCarrega As String
        Dim daCarrega As OleDbDataAdapter
        Dim dsCarrega As New DataSet()
        Try
            'abre conexao
            objConnP.Open()
            'monta select
            strCarrega = "SELECT cod_pesquisa, " & _
                         "desc_pesquisa " & _
                         "FROM pesquisas " & _
                         "ORDER BY desc_pesquisa"
            daCarrega = New OleDbDataAdapter(strCarrega, objConnP)
            'executa o comando
            daCarrega.Fill(dsCarrega, "Pesquisas")
            'monta o combo box
            With cbPesquisas
                .DataSource = dsCarrega.Tables("Pesquisas")
                .DisplayMember = "desc_pesquisa"
                .ValueMember = "cod_pesquisa"
            End With
            cbPesquisas.SelectedIndex = 0
            txtPergunta.Text = CType(cbPesquisas.SelectedValue, String)
        Catch erro As Exception
            MsgBox("Erro (008): " & erro.Message, MsgBoxStyle.Critical)
        Finally
            'fecha conexao
            objConnP.Close()
        End Try
    End Sub
    Private Sub cbPesquisas_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbPesquisas.SelectedIndexChanged
        'ativa o campo da pergunta
        txtPergunta.Enabled = True

        txtPergunta.Text = CType(cbPesquisas.SelectedValue, String)
    End Sub

Ele tá dando o seguinte erro:

Cast from type 'DataRowView' to type 'String' is not valid

Quem puder ajudar, obrigado.

Edição feita por: cacfs, 11/03/2005, 16:13.


#2 felipecm

felipecm

    Expert

  • Usuários
  • 541 posts
  • Sexo:Não informado
  • Localidade:ABC / SP

Posted 11/03/2005, 16:30

Estou tentando preenchendo um ComboBox com um DataSet que contem dados de uma BD. Quando tento detectar o valor selecionado, está dando erro.  Já tentei de tudo.
Quem puder me ajudar, segue o código abaixo:

    'carrega pesquisas
    Public Sub carrega_pesquisas()
        Dim strCarrega As String
        Dim daCarrega As OleDbDataAdapter
        Dim dsCarrega As New DataSet()
        Try
            'abre conexao
            objConnP.Open()
            'monta select
            strCarrega = "SELECT cod_pesquisa, " & _
                         "desc_pesquisa " & _
                         "FROM pesquisas " & _
                         "ORDER BY desc_pesquisa"
            daCarrega = New OleDbDataAdapter(strCarrega, objConnP)
            'executa o comando
            daCarrega.Fill(dsCarrega, "Pesquisas")
            'monta o combo box
            With cbPesquisas
                .DataSource = dsCarrega.Tables("Pesquisas")
                .DisplayMember = "desc_pesquisa"
                .ValueMember = "cod_pesquisa"
            End With
            cbPesquisas.SelectedIndex = 0
            txtPergunta.Text = CType(cbPesquisas.SelectedValue, String)
        Catch erro As Exception
            MsgBox("Erro (008): " & erro.Message, MsgBoxStyle.Critical)
        Finally
            'fecha conexao
            objConnP.Close()
        End Try
    End Sub
    Private Sub cbPesquisas_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbPesquisas.SelectedIndexChanged
        'ativa o campo da pergunta
        txtPergunta.Enabled = True

        txtPergunta.Text = CType(cbPesquisas.SelectedValue, String)
    End Sub

Ele tá dando o seguinte erro:

Cast from type 'DataRowView' to type 'String' is not valid

Quem puder ajudar, obrigado.

Bom, vamos la.. to sem ter como abrir o vs.net aqui.. mas va la

Experimenta tirar isso na hora que for popular o combo:
cbPesquisas.SelectedIndex = 0
txtPergunta.Text = CType(cbPesquisas.SelectedValue, String)

E também.. tenta utilizar ToString() ou CStr() ao invez do CType para conversão de tipos.
Troque em SelectedIndexChanged:
txtPergunta.Enabled = True
txtPergunta.Text = cbPesquisas.SelectedValue.ToString()

Edição feita por: felipecm, 11/03/2005, 16:31.

MCAD, MCP

#3 cacfs

cacfs

    Turista

  • Usuários
  • 50 posts
  • Sexo:Não informado

Posted 12/03/2005, 02:22

Troquei o CType() pelo ToString() como você falou e resolveu o problema. Porque estava dando esse erro com o CType()?

Obrigado pela ajuda!!!

#4 felipecm

felipecm

    Expert

  • Usuários
  • 541 posts
  • Sexo:Não informado
  • Localidade:ABC / SP

Posted 12/03/2005, 11:33

Troquei o CType() pelo ToString() como você falou e resolveu o problema. Porque estava dando esse erro com o CType()?

Obrigado pela ajuda!!!

Bom.. vc estava forçando uma conversão de tipos.. sendo que não era possivel. Sempre use o método ToString() da classe para printar dados, pois assim evita conversões.. e da uma olhadinha nesse material do Macoratti sobre casts com DirectCast e CType.. para vc saber diferenciá-los no futuro:

http://www.imasters....68767d28ca1467d

Ele diz assim num ponto:

Então, se você tem certeza de que a expressão é do mesmo tipo para o qual a conversão vai ser realizada use DirectCast ; se tiver dúvidas use Ctype.


Entao ale a pena dar uma olhadinha (y)
MCAD, MCP

#5 cacfs

cacfs

    Turista

  • Usuários
  • 50 posts
  • Sexo:Não informado

Posted 14/03/2005, 09:24

Valeu

#6 Miguceamma

Miguceamma

    MiguPenjisse

  • Usuários
  • 13201 posts

Posted 23/11/2017, 06:25

Levitra Strips Cialis Spezzato viagra Acheter Viagra Au Maroc
Priligy Duracion Efecto Purchase Lasix Online generic levitra 20mg Clomicalm Onit At Canadian Pharm
Amoxicillin Cure Vaginal Infection cialis Get Progesterone Menopause Canadian Pharms Levitra 10 Mag
Propecia Low Testosterone Doctors Daily Cialis Without Prescription Cialis Pas Cher viagra Canada Pharmacy No Prescription




0 user(s) are reading this topic

0 membro(s), 0 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq