3
votes

i am New With Vb.net, I use This Code To load A Combobox from a Mysql Database. Works good, but Slowly

Is there any way to Perform All This in One Query and load All Comboboxs Or Another Way To Perform This task Faster?

    AbrirDB()
    comandomysql.CommandText = "select distinct TblMarcas.marca from TblMarcas order by marca"
    Try
        Lector = comandomysql.ExecuteReader
        Lector.Read()
        .cbom.Properties.Items.Clear()
        If Lector.HasRows Then
            .cbom.Properties.Items.Add(Lector.Item("marca"))
            Do While Lector.Read
                .cbom.Properties.Items.Add(Lector.Item("marca"))
            Loop
        Else
        End If
    Catch ex As Exception
        DevExpress.XtraEditors.XtraMessageBox.Show(Err.Number & Err.Description)
        If conexion.State = ConnectionState.Open Then conexion.Close()
    End Try
    CerrarDb()




    'CARGO  Categorias a treelist
    AbrirDB()
    comandomysql.CommandText = "select distinct TblCategorias.categoria from TblCategorias order by categoria ASC"
    Try
        Lector = comandomysql.ExecuteReader
        Lector.Read()
        .cboc.Properties.Items.Clear()
        If Lector.HasRows Then
            .cboc.Properties.Items.Add(Lector.Item("categoria"))
            Do While Lector.Read
                .cbocategoria.Properties.Items.Add(Lector.Item("categoria"))
            Loop
        Else

        End If

    Catch ex As Exception
        DevExpress.XtraEditors.XtraMessageBox.Show(Err.Number & Err.Description)
        If conexion.State = ConnectionState.Open Then conexion.Close()
    End Try
    CerrarDb()


    'CARGO Subcategorias
    AbrirDB()
    comandomysql.CommandText = "select distinct TblSubcategorias.subcategoria from TblSubcategorias order by subcategoria ASC"
    Try
        Lector = comandomysql.ExecuteReader
        Lector.Read()
        .cbos.Properties.Items.Clear()
        If Lector.HasRows Then
            .cbos.Properties.Items.Add(Lector.Item("subcategoria"))
            Do While Lector.Read
                .cbos.Properties.Items.Add(Lector.Item("subcategoria"))
            Loop

        End If

    Catch ex As Exception
        DevExpress.XtraEditors.XtraMessageBox.Show(Err.Number & Err.Description)
        If conexion.State = ConnectionState.Open Then conexion.Close()
    End Try
    CerrarDb()

Any help would be appreciated.

1
Fill a DataTable and bind the CBO.DataSource to it Example: stackoverflow.com/a/36142887/1070452 - Ňɏssa Pøngjǣrdenlarp

1 Answers

0
votes

Try:
myCombo.SuspendLayout()
dim Table as new DataTable()
table.Load(MyDataReader)
myCombo.DataSource=table
myCombo.DisplayMember="column1"
myCombo.ResumeLayout()