In this article I will explain with an example, how to update data into Database 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.
Update data into Database in Windows Forms
 
I have already inserted few records in the table.
Update data into Database in Windows Forms
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Form Design
The following Form consists of:
Label – For labelling controls.
Button – For updating records.
Update data into Database 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="ConString" 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 in ASP.Net
When Update button is clicked, the CustomerId, Name and Country fields are fetched from their respective TextBoxes and the record is updated into the SQL Server database table using ADO.Net.
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.
 
Finally, based on whether record is updated or not an appropriate message displayed using MessageBox class.
C#
private void OnUpdate(object sender, EventArgs e)
{
    string query = "UPDATE Customers SET Name=@Name, Country=@Country WHERE CustomerId=@CustomerId";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            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 query As String = "UPDATE Customers SET Name=@Name, Country=@Country WHERE CustomerId=@CustomerId"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = Ne wSqlCommand(query)
            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
Update data into Database in Windows Forms
 
 
Downloads