In this article I will explain with an example, how to insert data into 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.
Insert with Stored Procedure into 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 will be used to Insert data into the MySQL database table.
This Stored Procedure accepts Name and Country parameters, which are used to Insert the records in Customers Table.
DELIMITER //
CREATE PROCEDURE Customers_InsertCustomer(
    IN Name VARCHAR(100),
    IN Country VARCHAR(50)
)
BEGIN
    INSERT INTO Customers(Name, Country)
    VALUES(Name, Country);
    SELECT LAST_INSERT_ID()
END//
DELIMITER ;
 
 

HTML Markup

The HTML Markup consists of following controls:
TextBox – For capturing Name to be inserted.
DropDownList – For selecting Country name to be inserted.
Button – For inserting the record.
The Button has been assigned with an OnClick event handler.
<table>
    <tr>
        <td>Name:</td>
        <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Country:</td>
        <td><asp:DropDownList ID="ddlCountries" runat="server">
                <asp:ListItem Text="Please select" Value=" "></asp:ListItem>
                <asp:ListItem Text="United States" Value="United States"></asp:ListItem>
                <asp:ListItem Text="India" Value="India"></asp:ListItem>
                <asp:ListItem Text="France" Value="France"></asp:ListItem>
                <asp:ListItem Text="Russia" Value="Russia"></asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="OnInsert" /></td>
    </tr>
</table>
 
 

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
 
 

Inserting with Stored Procedure into MySQL Database in ASP.Net

When Insert Button is clicked, 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.
 
The Name and Country values are fetched from the TextBox and DropDownList and passed as parameter.
Then, using ExecuteScalar method record is inserted into Customers Table of MySQL database using Stored Procedure.
Note: For more details on ExecuteScalar method, please refer my article Understanding MySqlCommand ExecuteScalar in C# and VB.Net.
 
Finally, the CustomerId of the inserted record is fetched and displayed in JavaScript Alert Message Box using RegisterStartupScript method.
C#
protected void OnInsert(object sender, EventArgs e)
{
    int customerId;
    string spName = "Customers_InsertCustomer";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = new MySqlCommand(spName, con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Name", txtName.Text);
            cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedItem.Text);
            con.Open();
            customerId = Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();
        }
    }
 
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Inserted Customer ID: " + customerId + "');",true);
}
 
VB.Net
Protected Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs)
    Dim customerId As Integer
    Dim spName As String = "Customers_InsertCustomer"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As MySqlConnection = New MySqlConnection(constr)
        Using cmd As MySqlCommand = New MySqlCommand(sql, con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@Name", txtName.Text)
            cmd.Parameters.AddWithValue("@Country", ddlCountries.SelectedItem.Text)
            con.Open()
            customerId = Convert.ToInt32(cmd.ExecuteScalar())
            con.Close()
        End Using
    End Using
 
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Inserted Customer ID: " & customerId & "');", True)
End Sub
 
 

Screenshots

The Form

Insert with Stored Procedure into MySQL Database in ASP.Net
 

Record after Insert in database

Insert with Stored Procedure into MySQL Database in ASP.Net
 
 

Downloads