In this article I will explain with an example, how to update data into 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.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure
This
Stored Procedure accepts
CustomerId,
Name 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)
AS
BEGIN
UPDATE Customers
SET [Name] = @Name,
[Country] = @Country
WHERE [CustomerId] = @CustomerId
END
Form Design
The following Form consists of:
Label – For labelling controls.
TextBox – For capturing the values of CustomerId, Name and Country.
Button – For updating records.
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>
Namespace
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
Updating the record in Database
When Update button is clicked, the connection string is fetched from the App.Config file and object of SqlConnection class is created using it.
Then, an object of SqlCommand class is created and the UPDATE query is passed to it as parameter.
The values of the CustomerId, Name and Country are added as parameter to SqlCommand object.
And the connection is opened and the ExecuteNonQuery function is executed.
Finally, based on whether record is updated or not, an appropriate message is displayed using MessageBox class.
C#
private void OnUpdate(object sender, EventArgs e)
{
string spName = "Customers_UpdateCustomer";
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.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
cmd.Connection = con;
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
MessageBox.Show("Customer record updated.");
}
else
{
MessageBox.Show("Customer not found.");
}
}
}
}
VB.Net
Private Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
Dim spName As String = "Customers_UpdateCustomer"
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.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
cmd.Connection = con
con.Open()
Dim i As Integer = cmd.ExecuteNonQuery()
con.Close()
If i > 0 Then
MessageBox.Show("Customer record updated.")
Else
MessageBox.Show("Customer not found.")
End If
End Using
End Using
End Sub
Screenshot
Downloads