In this article I will provide a simple tutorial with an example, how to use MySQL database in ASP.Net using C# and VB.Net.
 
 

Download and Install the MySQL Connector

You will need to download and install the MySQL Connector in order to connect to the MySQL database in Windows Forms Application.
Note: For details on how to download and install the MySQL Connector, please refer my article Download, install and reference MySQL Connector in C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Using MySQL database in ASP.Net Tutorial with example
 
I have already inserted few records in the table.
Using MySQL database in ASP.Net Tutorial with example
 
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:
GridView – For displaying data.

Columns

The GridView consists of three BoundField columns.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
 
 

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
 
 

Binding the GridView with records from MySQL Database Table

Inside the Page_Load event handler, first the connection is read from Web.Config file.
Note: For more details on how read connection string from Web.Config, please refer my article Read (Get) Connection String from Web.Config file in ASP.Net using C# and VB.Net.
 
Then, using the Fill method of the MySqlDataAdapter, the DataTable is populated which is later used to populate the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string sql = "SELECT CustomerId, Name, Country FROM Customers";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlDataAdapter sda = new MySqlDataAdapter(sql, con))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    gvCustomers.DataSource = dt;
                    gvCustomers.DataBind();
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As MySqlConnection = New MySqlConnection(constr)
            Using sda As MySqlDataAdapter = New MySqlDataAdapter(sql, con)
                Using dt As DataTable = New DataTable()
                    sda.Fill(dt)
                    gvCustomers.DataSource = dt
                    gvCustomers.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub
 
 

Screenshot

Using MySQL database in ASP.Net Tutorial with example
 
 

Demo

 
 

Downloads