In this article I will explain with an example, how to update data into 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.
Update using Dapper in ASP.Net
 
I have already inserted few records in the table.
Update using Dapper in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

HTML Markup

The following HTML Markup consists of:
TextBox – For capturing the values of CustomerId, Name and Country.
Button – For updating records.
The Button has been assigned with an OnClick event handler.
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 60px">Id<br />
            <asp:TextBox ID="txtId" runat="server" Width="50px" />
        </td>
        <td style="width: 150px">Name<br />
            <asp:TextBox ID="txtName" runat="server" Width="140px" />
        </td>
        <td style="width: 150px">Country:<br />
            <asp:TextBox ID="txtCountry" runat="server" Width="140px" />
        </td>
        <td style="width: 200px"><br />
            <asp:Button ID="btnUpdate" Text="Update" runat="server" OnClick="OnUpdate" />
        </td>
    </tr>
</table>
 
 

Namespaces

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

Updating using Dapper in ASP.Net

When Update 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 Execute method of Dapper library record is updated into the SQL Server database.
Note: For more details on Execute method, please refer my article Understanding Dapper Execute in C# and VB.Net.
 
Finally, based on whether record is updated or not an appropriate message is displayed in JavaScript Alert Message Box using RegisterStartupScript method.
C#
protected void OnUpdate(object sender, EventArgs e)
{
    string sql = "UPDATE Customers SET Name=@Name, Country=@Country WHERE CustomerId=@CustomerId";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        int customerId = int.Parse(txtId.Text);
        string name = txtName.Text;
        string country = txtCountry.Text;
        int i = con.Execute(sql, new { customerId, name, country });
        if (i > 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer record updated.');", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Customer not found.');", true);
        }
    }
}
 
VB.Net
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
    Dim sql As String = "UPDATE Customers SET Name=@Name, Country=@Country WHERE CustomerId=@CustomerId"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Dim customerId As Integer = Integer.Parse(txtId.Text)
        Dim name As String = txtName.Text
        Dim country As String = txtCountry.Text
        Dim i As Integer = con.Execute(sql, New With {customerId, name, country})
        If i > 0 Then
            ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer record updated.');", True)
        Else
            ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Customer not found.');", True)
        End If
    End Using
End Sub
 
 

Screenshot

Update using Dapper in ASP.Net
 
 

Downloads