In this article I will explain with an example, how to update data into database with Stored Procedure using Dapper library in Windows Forms (WinForms) Application 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.
Update with Stored Procedure using Dapper in C# and VB.Net
I have already inserted few records in the table.
Update with Stored Procedure using Dapper in 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 Stored Procedure will be used to Update data into the SQL Server database table.
This Stored Procedure accepts CustomerIdName and Country parameters, which are used to UPDATE the records in Customers Table.
CREATE PROCEDURE [Customers_UpdateCustomer]
      @CustomerId INT
     ,@Name VARCHAR(100)
     ,@Country VARCHAR(50)
    UPDATE Customers
    SET [Name] = @Name,
        [Country] = @Country
    WHERE [CustomerId] = @CustomerId

Form Design

The following Form consists of:
Label – For labelling the controls.
TextBox – For capturing the values of CustomerId, Name and Country.
Button – For updating records.
Update with Stored Procedure using Dapper in C# and VB.Net


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

Updating with Stored Procedure using Dapper

When Update button is clicked, first the connection is read from App.Config file.
Note: For more details on how to read connection string from App.Config file, please refer my article .Net 4.5: Read (Get) Connection String from App.Config file using C# and VB.Net.
Then, using Execute method of Dapper library with Stored Procedure 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 using MessageBox class.
private void OnUpdate(object sender, EventArgs e)
    string spName = "Customers_UpdateCustomer";
    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(spName, new { customerId, name, country }, commandType: CommandType.StoredProcedure);
        if (i > 0)
            MessageBox.Show("Customer record updated.");
            MessageBox.Show("Customer not found.");
        txtId.Text = string.Empty;
        txtName.Text = string.Empty;
        txtCountry.Text = string.Empty;
Private Sub OnUpdate(sender As Object, e As EventArgs) Handles btnUpdate.Click
    Dim spName As String = "Customers_UpdateCustomer"
    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(spName, New With {customerId, name, country}, commandType:=CommandType.StoredProcedure)
        If i > 0 Then
            MessageBox.Show("Customer record updated.")
            MessageBox.Show("Customer not found.")
        End If
        txtId.Text = String.Empty
        txtName.Text = String.Empty
        txtCountry.Text = String.Empty
    End Using
End Using


Update with Stored Procedure using Dapper in C# and VB.Net