In this article I will explain with an example, how to Delete data from database using Stored Procedure in Windows Forms (WinForms) Applications using C# and VB.Net.
I have made use of the following table Customers with the schema as follows.
Delete data from Database using Stored Procedure in Windows Forms
I have already inserted few records in the table.
Delete data from Database using Stored Procedure in Windows Forms
Stored Procedure
The following Stored Procedure will be used to Delete data from the SQL Server database table.
This Stored Procedure accepts CustomerId parameter, which is used to DELETE the records in Customers Table.
CREATE PROCEDURE [Customers_DeleteCustomer]
      @CustomerId INT
      DELETE FROM [Customers]
      WHERE CustomerId = @CustomerId
Form Design
The following Form consists of:
TextBox – For capturing CustomerId of the Customer record to be deleted.
Button – For deleting the record.
Delete data from Database using Stored Procedure in Windows Forms
Adding ConnectionString to the App.Config file
You need to add the Connection String in the ConnectionStrings section of the App.Config file in the following way.
<?xml version="1.0" encoding="utf-8" ?>
        <add name="constr" connectionString="Data Source=Mudassar-PC\SQL2019;Initial Catalog=AjaxSamples;Integrated Security=true" />
You will need to import the following namespaces.
using System.Configuration;
using System.Data.SqlClient;
Imports System.Configuration
Imports System.Data.SqlClient
Deleting the record from Database using Stored Procedure in Windows Forms
When Delete button is clicked, the connection string is fetched from the App.Config file and object of SqlConnection class is created using it.
Note: For more details on how to read Connection String from App.Config file, please refer my article Read (Get) Connection String from App.Config file using C# and VB.Net.
Then, an object of SqlCommand class is created and the DELETE query is passed to it as parameter.
The value of the CustomerId is added as parameter to SqlCommand object.
And the connection is opened and the ExecuteNonQuery function is executed.
Note: For more details on how to use ExecuteNonQuery function, please refer Understanding SqlCommand ExecuteNonQuery in C# and VB.Net.
Finally, based on whether record is deleted or not, an appropriate message displayed using MessageBox class.
private void OnDelete(object sender, EventArgs e)
    string spName = "Customers_DeleteCustomer";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
        using (SqlCommand cmd = new SqlCommand(spName))
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CustomerId", txtId.Text);
            cmd.Connection = con;
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
                MessageBox.Show("Customer record deleted.");
                MessageBox.Show("Customer not found.");
Private Sub OnDelete(sender As Object, e As EventArgs) Handles btnDelete.Click
    Dim spName As String = "Customers_DeleteCustomer"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(spName)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@CustomerId", txtId.Text)
            cmd.Connection = con
            Dim i As Integer = cmd.ExecuteNonQuery()
            If i > 0 Then
                MessageBox.Show("Customer record deleted.")
                MessageBox.Show("Customer not found")
            End If
        End Using
    End Using
End Sub
Delete data from Database using Stored Procedure in Windows Forms