In this article I will explain with an example, how to use
ExecuteNonQuery with
Output Parameters examples in
SQL Server Stored Procedure in Windows Forms (WinForms) Application using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure
This
Stored Procedure accepts following parameters which are used to
insert the records:
Input Parameters
Name – It is used to pass the Name of the Customer.
Country – It is used to pass the Country of the Customer.
Output Parameters
CustomerId – It is used to get the last inserted CustomerId.
Note: Output Parameter is identified by the keyword OUTPUT.
CREATE PROCEDURE [Customers_InsertCustomer_Output]
@Name VARCHAR(100),
@Country VARCHAR(50),
@CustomerId INT OUTPUT
AS
BEGIN
INSERT INTO [Customers]
([Name]
,[Country])
VALUES
(@Name
,@Country)
SELECT @CustomerId = SCOPE_IDENTITY()
END
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.
Button – For inserting records.
The Button has been assigned with a Click event handler.
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=.\SQL2022;Initial Catalog=AjaxSamples;Integrated Security=true" />
</connectionStrings>
</configuration>
Namespace
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Using ExecuteNonQuery with Output Parameters
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 an object of SqlConnection class is created using it.
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 i.e. CustomerId is added as parameter to SqlCommand class object and its DataType is set and the Direction is set to Output.
Note: The default the Direction of a Parameter is INPUT.
Then, the connection is opened and the ExecuteNonQuery method is executed.
Once the ExecuteNonQuery method is executed, the value of the CustomerId is fetched from the Value property of the CustomerId Output Parameter.
Finally, the TextBox and ComboBox values are set to empty and CustomerId of the inserted record is displayed in MessageBox.
C#
private void Form1_Load(object sender, EventArgs e)
{
cbCountries.Items.Add("United States");
cbCountries.Items.Add("India");
cbCountries.Items.Add("France");
cbCountries.Items.Add("Russia");
}
private void OnInsert(object sender, EventArgs e)
{
string spName = "Customers_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;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
string customerId = Convert.ToString(cmd.Parameters["@CustomerId"].Value);
txtName.Text = string.Empty;
cbCountries.Text = string.Empty;
MessageBox.Show"Inserted Customer ID: " + customerId);
}
}
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cbCountries.Items.Add("United States")
cbCountries.Items.Add("India")
cbCountries.Items.Add("France")
cbCountries.Items.Add("Russia")
End Sub
Private Sub OnInsert(ByVal sender As Object, ByVal e As EventArgs) Handles btnInsert.Click
Dim spName As String = "Customers_InsertCustomer_Output"
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("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedItem)
cmd.Parameters.Add("@CustomerId", SqlDbType.Int)
cmd.Parameters("@CustomerId").Direction = ParameterDirection.Output
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
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
Screenshots
Record after Insert in database
Downloads