In this article I will explain with an example, how to use Output parameter in SQL Server Stored Procedure in Windows Forms (WinForms) Application using C# and VB.Net.
I have made use of the following table Customers with the schema as follows.
Using Output Parameter in SQL Server Stored Procedure 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 Insert data into the SQL Server database table.
This Stored Procedure accepts following parameters which are used to insert the records:
Name – This is an INPUT Parameter to pass the Name of the Customer.
Country – This is an INPUT Parameter to pass the Country of the Customer.
CustomerId - This is an OUTPUT Parameter to get the last inserted CustomerId.
Note: Output Parameter is identified by the keyword OUTPUT.
CREATE PROCEDURE [Customer_InsertCustomer_Output]
      @Name VARCHAR(100),
      @Country VARCHAR(50),
      @CustomerId INT OUTPUT
      INSERT INTO [Customers]
      SELECT @CustomerId = SCOPE_IDENTITY()
Form Design
The following Form consists of:
Label – For labelling controls.
TextBox – For capturing Name to be inserted.
ComboBox – For capturing Country Name to be inserted.
Using Output Parameter in SQL Server Stored Procedure in C# and VB.Net
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
Inserting record into the Database using Stored Procedure in Windows Forms
Inside the Form Load event handler, the ComboBox items are added.
When Insert 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 INSERT query is passed to it as parameter.
The values of the Name and Country are added as parameter to SqlCommand object.
Next, the Output Parameter CustomerId is added using the Add method of SqlCommand Parameter class with its Data Type and Size specified and the Direction is set to Output since by default the Direction of all Parameter is Input.
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.
Once the Stored Procedure is executed, the value of the CustomerId of the inserted record is fetched from the Value property of the CustomerId Output Parameter.
Finally, the TextBox and ComboBox value is set to empty and CustomerId of the inserted record is displayed in MessageBox.
private void Form1_Load(object sender, EventArgs e)
    cbCountries.Items.Add("Please Select");
    cbCountries.Items.Add("United States");
private void OnInsert(object sender, EventArgs e)
    string spName = "Customer_InsertCustomer_Output";
    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("@Name", txtName.Text);
            cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedItem);
            cmd.Parameters.Add("@CustomerId", SqlDbType.Int);
            cmd.Parameters["@CustomerId"].Direction = ParameterDirection.Output;
            cmd.Connection = con;
            string customerId = Convert.ToString(cmd.Parameters["@CustomerId"].Value);
            txtName.Text = string.Empty;
            cbCountries.Text = string.Empty;
            MessageBox.Show("Inserted Customer ID: " + customerId);
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    cbCountries.Items.Add("Please Select")
    cbCountries.Items.Add("United States")
End Sub
Private Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click
    Dim spName As String = "Customer_InsertCustomer_Output"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con AsSqlConnection = New SqlConnection(constr)
        Using cmd AsSqlCommand = New SqlCommand(spName)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@Name", txtName.Text)
            cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedItem)
            cmd.Parameters.Add("@CustomerId", SqlDbType.Int)
            cmd.Parameters("@CustomerId").Direction = ParameterDirection.Output
            cmd.Connection = con
            Dim customerId As String = Convert.ToString(cmd.Parameters("@CustomerId").Value)
            txtName.Text = String.Empty
            cbCountries.Text = String.Empty
            MessageBox.Show("Inserted Customer ID: " & customerId)
        End Using
    End Using
End Sub
Using Output Parameter in SQL Server Stored Procedure in C# and VB.Net
Record after Insert in database
Using Output Parameter in SQL Server Stored Procedure in C# and VB.Net