In this article I will explain with an example, how to bind / fill / populate ListBox control from MySQL database in ASP.Net using C# and VB.Net.
The ASP.Net ListBox will be populated with MySQL database using the MySQL Connector.
Note: For more details on how to use MySQL database, please refer my article How to use MySQL Connector in ASP.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Bind / Fill / Populate ListBox control from MySql database in ASP.Net using C# and VB.Net
 
I have already inserted few records in the table.
Bind / Fill / Populate ListBox control from MySql database in ASP.Net using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
           Download SQL file
 
 

HTML Markup

The HTML Markup consists of following control:
ListBox – For displaying the records.
<asp:ListBox ID="lstCustomers" runat="server"></asp:ListBox
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.Configuration;
using MySql.Data.MySqlClient;
 
VB.Net
Imports System.Configuration
Imports MySql.Data.MySqlClient
 
 

Bind / Fill / Populate ListBox control from MySQL database

Inside the Page_Load event handler, first the connection is read from Web.Config file.
Note: For more details on how to read connection string from Web.Config file, please refer my article Read or Write Connection Strings in Web.Config file using ASP.Net using C# and VB.Net.
 
Then, the records are fetched from the Customers Table of MySQL database using ExecuteReader method.
Note: For more details on ExecuteReader method, please refer my article Using MySqlCommand ExecuteReader Example in ASP.Net with C# and VB.NetMySqlCommand ExecuteReader Example in ASP.Net with C# and VB.Net.
 
The ExecuteReader method is called which is assigned to the DataSource property of ListBox control.
Then, the Name and CustomerId values are assigned to the DataTextField and DataValueField properties of the ListBox control.
DataTextField – The values set as DataTextField are visible to the user.
DataValueField – The values set as DataValueField are not visible to the user. Generally ID or Primary Key columns are set as values in order to uniquely identify a ListBox Item.
Finally, the ListBox control is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string sql "SELECT CustomerId, Name FROM Customers";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand(sql, con))
            {
                con.Open();
                lstCustomers.DataSource cmd.ExecuteReader();
                lstCustomers.DataTextField "Name";
                lstCustomers.DataValueField "CustomerId";
                lstCustomers.DataBind();
                con.Close();
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim sql As String "SELECT CustomerId, Name FROM Customers"
        Dim constr As String ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New MySqlConnection(constr)
            Using cmd As New MySqlCommand(sql, con)
                con.Open()
                lstCustomers.DataSource cmd.ExecuteReader()
                lstCustomers.DataTextField "Name"
                lstCustomers.DataValueField "CustomerId"
                lstCustomers.DataBind()
                con.Close()
            End Using
        End Using
    End If
End Sub
 
 

Screenshot

Bind / Fill / Populate ListBox control from MySql database in ASP.Net using C# and VB.Net
 
 

Demo

 
 

Downloads