In this article I will explain with an example, how to insert data into MySQL database using Dapper library in ASP.Net using C# and VB.Net.
 
 

Installing Dapper package using Nuget

In order to install Dapper library using Nuget, please refer my article Install Dapper from Nuget in Visual Studio.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Insert using Dapper into MySQL Database in ASP.Net
 
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 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 Dapper;
using System.Configuration;
using MySql.Data.MySqlClient;
 
VB.Net
Imports Dapper
Imports System.Configuration
Imports MySql.Data.MySqlClient
 
 

Inserting using Dapper from 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.
 
Then, the SQL query and an object with Name and Country values are passed to the ExecuteScalar method of the Dapper library which then inserts the record into the Customers Table and CustomerId of the inserted record is fetched.
Note: For more details on how to use ExecuteScalar method, please refer my article Understanding Dapper ExecuteScalar in MySQL in C# and VB.Net.
 
Finally, the CustomerId of the inserted record is displayed in JavaScript Alert Message Box using RegisterStartupScript method and TextBox, DropDownList is set to empty.
C#
protected void OnInsert(object sender, EventArgs e)
{
    int customerId;
    string sql = "INSERT INTO Customers (Name, Country) VALUES (@Name, @Country);";
    sql += "SELECT LAST_INSERT_ID();";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        string name = txtName.Text;
        string country = ddlCountries.SelectedValue;
        customerId = Convert.ToInt32(con.ExecuteScalar(sql, new { name, country }));
    }
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Inserted Customer ID: " + customerId + "');", true);
 
    txtName.Text = string.Empty;
    ddlCountries.SelectedValue = string.Empty;
}
 
VB.Net
Protected Sub OnInsert(sender As Object, e As EventArgs)
    Dim customerId As Integer
    Dim sql As String = "INSERT INTO Customers (Name, Country) VALUES (@Name, @Country);"
    sql += " SELECT LAST_INSERT_ID();"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As MySqlConnection = New MySqlConnection(constr)
        Dim name As String = txtName.Text
        Dim country As String = ddlCountries.SelectedValue
        customerId = Convert.ToInt32(con.ExecuteScalar(sql, New With {name, country}))
    End Using
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Inserted Customer ID: " & customerId & "');", True)
 
    txtName.Text = String.Empty
    ddlCountries.SelectedValue = String.Empty
End Sub
 
 

Screenshots

The Form

Insert using Dapper into MySQL Database in ASP.Net
 

Record after Insert in database

Insert using Dapper into MySQL Database in ASP.Net
 
 

Downloads