'Adding Simple Progress Bar while inserting Data to SQL Server in C# windows form

I need to add a simple progress bar while inserting some data to a sql server database. my code works to insert data but I need to add progress bar showing progress while inserting that data from start to end. So I am not sure how to start to add the progress bar and end it. Any advice would be greatly appreciated

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using PasswordLogs.Data;

namespace PasswordLogs
{

public partial class Form1 : Form
{
    private readonly CurrentUser _currentUserOnApp = new CurrentUser();

    public Form1()
    {
        InitializeComponent();
    }

    private void Button_Update_Click(object sender, EventArgs e)
    {
        GetDataInformation();
        InsertDataToDb();
    }

    private void Button_Clear_Click(object sender, EventArgs e)
    {
        Clear();
    }

    public void GetDataInformation()
    {

        try
        {
            if (Combo_ServiceType.SelectedItem == null && string.IsNullOrWhiteSpace(Text_Username.Text) && string.IsNullOrWhiteSpace(Text_Password.Text) && string.IsNullOrWhiteSpace(Text_OldPassword.Text))
            {
                MessageBox.Show(@"(Service Type, Username, Password, Old Password) cannot be null or empty", @"User Information", MessageBoxButtons.OKCancel);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

    public void Clear()
    {
        Text_OldPassword.Clear();
        Text_Password.Clear();
        Text_Description.Clear();
        Text_Notes.Clear();
        Text_Username.Clear();
        Combo_ServiceType.SelectedIndex = -1;
        Combo_ServiceType.Focus();
    }

    public void InsertDataToDb()
    {
        var connection = ConfigurationManager.ConnectionStrings["LogsDbContext"].ConnectionString;

        try
        {
            using (var sqlConnection = new SqlConnection(connection))
            {
                sqlConnection.Open();

                using (var command = new SqlCommand())
                {

                    command.Connection = sqlConnection;
                    command.CommandType = CommandType.Text;
                    command.CommandText = "INSERT INTO dbo.EntryLogs (ServiceType, ServiceIP, Username, Password, OldPassword, Description, CreatedSysUser, Notes) VALUES (@ServiceType, @ServiceIP, @Username, @Password, @OldPassword, @Description, @CreatedSysUser, @Notes)";

                    command.Parameters.AddWithValue("@ServiceType", Combo_ServiceType.SelectedItem.ToString());
                    command.Parameters.AddWithValue("@ServiceIP", string.IsNullOrWhiteSpace(Text_ServiceIP.Text) ? (object)DBNull.Value : Text_ServiceIP.Text);
                    command.Parameters.AddWithValue("@Username", Text_Username.Text);
                    command.Parameters.AddWithValue("@Password", Text_Password.Text);
                    command.Parameters.AddWithValue("@OldPassword", Text_OldPassword.Text);
                    command.Parameters.AddWithValue("@Description", string.IsNullOrWhiteSpace(Text_Description.Text) ? (object)DBNull.Value : Text_Description.Text);
                    command.Parameters.AddWithValue("@CreatedSysUser", _currentUserOnApp.GetCurrentUsernameOnApp());
                    command.Parameters.AddWithValue("@Notes", string.IsNullOrWhiteSpace(Text_Notes.Text) ? (object)DBNull.Value : Text_Notes.Text);

                    var result = command.ExecuteNonQuery();

                    // Check Error
                    if (result < 0)
                    {
                        MessageBox.Show(@"Error inserting data into Database!", @"Error Inserting Data", MessageBoxButtons.YesNoCancel);
                    }

                    MessageBox.Show(@"Record inserted to database!", @"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    sqlConnection.Close();

                }
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(@"ERROR: ", e.Message);
        }
    }

}

}



Solution 1:[1]

Use background worker.

double perc;
int count = 0;

Then add this code before the loop does.

count++;

//Create an extension method on this one that only accept up to 100, there's no 101%.

perc = Math.Round((Convert.ToDouble(count) / Convert.ToDouble(overall)) * 100); 
yourbackgroundworker.ReportProgress((int)perc);

On ProgressChange:

progressbar.value = e.ProgressPercentage;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bill Tür stands with Ukraine