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.
 
 
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
 
 
Download and Install the MySQL Connector
You will need to download and install the MySQLConnector in order to connect to the MySQL database in ASP.Net. For more details refer How to use MySQL Connector in ASP.Net.
 
 
HTML Markup
The HTML Markup consists of an ASP.Net ListBox which will be populated from MySql database.
<asp:ListBox ID="lstCustomers" runat="server">
</asp:ListBox>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
 
 
Bind / Fill / Populate ListBox control from MySql database in ASP.Net
Inside the Page Load event of the page, the ListBox is populated with the records of the Customers Table of the MySql database.
The CustomerId and the Name column values are fetched from the database using SqlDataReader and are assigned to the DataTextField and DataValueField properties of the ListBox control.
DataTextField – The values of the Column set as DataTextField are visible to the user.
DataValueField – The values of the Column 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.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand("SELECT CustomerId, Name FROM Customers"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = 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 constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New MySqlConnection(constr)
            Using cmd As New MySqlCommand("SELECT CustomerId, Name FROM Customers")
                cmd.CommandType = CommandType.Text
                cmd.Connection = 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