Show Two Values from Database in Combobox VB.NET

Last Updated on November 10, 2022 by Humera Hallari

Here the code if you want to get two values from database, and show in Combobox item in Visual Basic VB.NET.

Module:

Imports MySql.Data.MySqlClient
Public Class DTConnection
    Dim conect As New MySqlConnection("server=localhost;
    user=root;
    password=;
    database=combobox_db;")
    Public Function Open() As MySqlConnection
        Try
            conect.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return conect
    End Function
    Public Function Close() As MySqlConnection
        Try
            conect.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return conect
    End Function
End Class

Form:

Imports MySql.Data.MySqlClient
Public Class Form1
    Dim mycmd As New MySqlCommand
    Dim myconnection As New DTConnection
    Dim objdatareader As MySqlDataReader
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        myconnection.Close()
        mycmd = myconnection.Open.CreateCommand
        mycmd.CommandText = "select number,description from tbdetail"
        objdatareader = mycmd.ExecuteReader
        While objdatareader.Read
            ComboBox1.Items.Add("[" & objdatareader(0) & "]" & " - " & objdatareader(1))
            ComboBox1.ValueMember = objdatareader(0)
        End While
        myconnection.Close()
    End Sub
End Class

Leave a Comment