In this article I will explain with an example, how to Insert data into database with Stored Procedure using Dapper 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.


I have made use of the following table Customers with the schema as follows.
Insert with Stored Procedure using Dapper 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 SQL Server database table.
This Stored Procedure accepts Name and Country parameters, which are used to Insert the records in Customers Table.
CREATE PROCEDURE [Customers_InsertCustomer]
     @Name VARCHAR(100),
     @Country VARCHAR(50)
    INSERT INTO [Customers]

HTML Markup

The following HTML Markup consists of:
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.
        <td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></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>
        <td><asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="OnInsert" /></td>


You will need to import the following namespaces.
using Dapper;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Imports Dapper
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Inserting with Stored Procedure using Dapper 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, using ExecuteScalar method of Dapper library with Stored Procedure record is inserted into the SQL Server database.
Note: For more details on ExecuteScalar method, please refer my article Understanding Dapper 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, TextBox and DropDownList value are set to empty.
protected void OnInsert(object sender, EventArgs e)
    int customerId;
    string spName = "Customers_InsertCustomer";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
        string name = txtName.Text;
        string country = ddlCountries.SelectedValue;
        customerId = Convert.ToInt32(con.ExecuteScalar(spName, new { name, country }, commandType: CommandType.StoredProcedure));
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Inserted Customer ID: " + customerId + "');", true);
    txtName.Text = string.Empty;
    ddlCountries.SelectedValue = string.Empty;
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 SqlConnection = New SqlConnection(constr)
        Dim name As String = txtName.Text
        Dim country As String = ddlCountries.SelectedValue
        customerId = Convert.ToInt32(con.ExecuteScalar(spName, New With {name, country}, commandType:=CommandType.StoredProcedure))
    End Using
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Inserted Customer ID: " & customerId & "');", True)
    txtName.Text = String.Empty
    ddlCountries.SelectedValue = String.Empty
End Sub


The Form

Insert with Stored Procedure using Dapper in ASP.Net

Record after Insert in database

Insert with Stored Procedure using Dapper in ASP.Net