In this article I will explain how to call MySql Stored Procedure with Parameters in ASP.Net, C# and VB.Net.
The article explains with a simple example where a Stored Procedure will be passed with a parameter and it returns the matching records. In similar way one can pass multiple parameters.
Using MySQL with ASP.Net
This portion is extensively covered in my article Use and connect to MySQL Database in ASP.Net Application using MySQLConnector.
I have made use of the following table Customers with the schema as follows.
Call MySql Stored Procedure with Parameters in ASP.Net, C# and VB.Net
I have already inserted few records in the table.
Call MySql Stored Procedure with Parameters in ASP.Net, C# and VB.Net
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file
Stored Procedure
The following MySql Stored Procedure accepts a parameter custId Integer parameter and is matched with the CustomerId field of the Customers Table.
The matching record is returned by the Stored Procedure.
CREATE DEFINER=`root`@`localhost` PROCEDURE `Customers_GetCustomer`(IN custId INT)
       SELECT Name
       FROM Customers
       WHERE CustomerId = custId;
HTML Markup
The HTML Markup consists of an ASP.Net TextBox, a Button and a GridView control.
<asp:TextBox ID="txtCustomerId" runat="server" Text="" />
<asp:Button Text="Search" runat="server" OnClick="Search" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
You will need to import the following namespaces.
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
Calling MySql Stored Procedure with Parameters in ASP.Net, C# and VB.Net
When the Search Button is clicked, it calls the GetCustomer function which accepts the CustomerId entered in the TextBox and passes it as parameter to the MySql Stored Procedure.
The records returned by the Stored Procedure are displayed in the GridView.
protected void Search(object sender, EventArgs e)
private void GetCustomer(int customerId)
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
        using (MySqlCommand cmd = new MySqlCommand("Customers_GetCustomer", con))
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CustId", customerId);
            using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
                DataTable dt = new DataTable();
                GridView1.DataSource = dt;
Protected Sub Search(sender As Object, e As EventArgs)
End Sub
Private Sub GetCustomer(customerId As Integer)
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New MySqlConnection(constr)
        Using cmd As New MySqlCommand("Customers_GetCustomer", con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@CustId", customerId)
            Using sda As New MySqlDataAdapter(cmd)
                Dim dt As New DataTable()
                GridView1.DataSource = dt
            End Using
        End Using
    End Using
End Sub
Call MySql Stored Procedure with Parameters in ASP.Net, C# and VB.Net