In this article I will explain with an example, how to select data from MySQL database with Stored Procedure in ASP.Net using C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Select with Stored Procedure from MySQL Database in ASP.Net
 
I have already inserted few records in the table.
Select with Stored Procedure from MySQL Database in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Stored Procedure

The following Stored Procedure is used to display the records from MySQL database.
DELIMITER //
CREATE PROCEDURE Customers_GetCustomers()
BEGIN
    SELECT CustomerId, Name, Country
    FROM Customers
END //
DELIMITER ;
 
 

HTML Markup

The HTML Markup consists of following control:
GridView – For displaying data.
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
 
 

Selecting with Stored Procedure from MySQL Database in ASP.Net

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, a connection to the database is established using the MySqlConnection class and the records are fetched from the Customers Table of MySQL database using Stored Procedure.
Finally, the DataTable is assigned to the DataSource property of GridView and the GridView is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack)
    {
        string spName = "Customers_GetCustomers";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand(spName, con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
                {
                    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 spName As String = "Customers_GetCustomers"
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As MySqlConnection = New MySqlConnection(constr)
            Using cmd As MySqlCommand = New MySqlCommand(spName, con)
                cmd.CommandType = CommandType.StoredProcedure
                Using sda As MySqlDataAdapter = New MySqlDataAdapter(cmd)
                    Using dt As DataTable = New DataTable()
                        sda.Fill(dt)
                        gvCustomers.DataSource = dt
                        gvCustomers.DataBind()
                    End Using
                End Using
            End Using
        End Using
    End If
End Sub
 
 

Screenshot

Select with Stored Procedure from MySQL Database in ASP.Net
 
 

Downloads