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.
 
 
Database
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
AS
BEGIN
      DELETE FROM [Customers]
      WHERE CustomerId = @CustomerId
END
 
 
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" ?>
<configuration>
    <connectionStrings>
        <add name="constr" connectionString="Data Source=Mudassar-PC\SQL2019;Initial Catalog=AjaxSamples;Integrated Security=true" />
    </connectionStrings>
</configuration>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
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.
C#
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;
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
 
            if (i > 0)
            {
                MessageBox.Show("Customer record deleted.");
            }
            else
            {
                MessageBox.Show("Customer not found.");
            }
        }
    }
}
 
VB.Net
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
            con.Open()
            Dim i As Integer = cmd.ExecuteNonQuery()
            con.Close()
            If i > 0 Then
                MessageBox.Show("Customer record deleted.")
            Else
                MessageBox.Show("Customer not found")
            End If
        End Using
    End Using
End Sub
 
 
Screenshot
Delete data from Database using Stored Procedure in Windows Forms
 
 
Downloads