VB.NET, show in combobox the data of a select query

0

Hello good night everyone, I have a method that allows you to search for a value, if you find it, it brings me the associated data and passes it to different controls, to the textbox, labels no drama, but to show in the combobox it does not work, it does not show the value in the combobox. the method is this:

Try

        Dim confirmacion As DialogResult

        Dim queryBusqueda As String = "SELECT * FROM tbl_funcionario_soporte WHERE rut_Funcionario=@rut_funcionario"

        comando = New MySqlCommand(queryBusqueda, conexionMYSQL)

        comando.Parameters.AddWithValue("@rut_funcionario", txtRut.Text)

        da.SelectCommand = comando

        da.Fill(dt)

        If (dt.Rows.Count <> 0) Then
            cmdEliminar.Enabled = True
            cmdActualizar.Enabled = True
            Button5.Enabled = False
            Button1.Enabled = False
            txtRut.Enabled = True
            txtNombre.Text = dt.Rows(0)(4).ToString
            txtIdDpto.Text = dt.Rows(0)(1).ToString
            cmbDepartamento.SelectedItem = dt.Rows(0)(5).ToString
            'cmbDepartamento.SelectedValue = dt.Rows(0)(1).ToString
            'txtIdProfesion.Text = dt.Rows(0)(1).ToString
            ComboBox1.SelectedValue = dt.Rows(0)(1).ToString
            txtDireccion.Text = dt.Rows(0)(6).ToString
            txtCelular.Text = dt.Rows(0)(7).ToString
            txtAnexo.Text = dt.Rows(0)(8).ToString
            txtEmail.Text = dt.Rows(0)(9).ToString
            txtcontraseña.Text = dt.Rows(0)(10).ToString
            TextBox1.Text = dt.Rows(0)(2).ToString
            'cmbTipoPermiso.SelectedValue = dt.Rows(0)(6).ToString
            'btnNuevaBusqueda.Visible = True

            lblBuscador.Visible = False

            desbloquearEdicion()

            conexionMYSQL.Close()
        Else
            confirmacion = MessageBox.Show("Rut no Existe en la Base de Datos ¿Desea Registrarlo Ahora?", "Sistema de Gestión de Incidencias", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)

            If confirmacion = Windows.Forms.DialogResult.OK Then
                Try
                    desbloqearControles()
                Catch ex As Exception
                    MsgBox(ex.Message)
                Finally
                    conexionMYSQL.Close()
                End Try
            ElseIf confirmacion = Windows.Forms.DialogResult.Cancel Then
                conexionMYSQL.Dispose()
                txtRut.Text = ""
                txtRut.Focus()
                Return
            End If
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        conexionMYSQL.Dispose()
    End Try
    conexionMYSQL.Close()

and pass the values assigned in this way:

            txtIdDpto.Text = dt.Rows(0)(1).ToString
            cmbDepartamento.SelectedItem = dt.Rows(0)(5).ToString

THE METHOD I USE to load the combobox (I call it in the form load) is this:

  Public Sub cargaDepartamentos()

    'LIMPIAMOS Y ACTUALZIAMOS EL COMBOBOX
    Me.cmbDepartamento.Items.Clear()

    conexionMYSQL = New MySqlConnection

    conexionMYSQL.ConnectionString = ("server=localhost;User Id=soporte;database=db_incidencias_muniquel;password=123")

    Dim dr As MySqlDataReader

    Dim comando As MySqlCommand

    Try

        conexionMYSQL.Open()

        Dim query As String = ("SELECT id_departamento, nombre_departamento FROM tbl_departamentos")

        comando = New MySqlCommand(query, conexionMYSQL)

        dr = comando.ExecuteReader

        While dr.Read
            Dim nombre_departamento = dr.GetString("nombre_departamento")

            cmbDepartamento.Items.Add(nombre_departamento)
        End While

        conexionMYSQL.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        conexionMYSQL.Dispose()
    End Try

End Sub

Greetings to all, have an excellent day and from now on, thanks for the help

    
asked by Nicolas Ezequiel Almonacid 26.04.2018 в 22:19
source

0 answers