Thursday, 11 August 2016

ProgressBar While Data Transfer in SQL Server Table in Windows Application Using C#.Net

8/11/2016 - By Pranav Singh 0


This article will show you how you can show progress bar while data transfer from one sql server table to other sql server table in windows application using c#.net on button click event.


So for this article first we will create a new windows application and add a progressbar control and a backgroundWorker control.


backgroundWorker control


Now we will generate the ProgressChanged event and DoWork event of backgroundWorker.



Now we will create the function to get the data from the database .

   public DataTable GetTable1Data()
        {
            SqlConnection con = new SqlConnection("---Your connection string----");
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter("Select Id,Name,Address from Table1", con);
            con.Open();
            da.Fill(dt);
            return dt;
        }

Now we will create a function to save the data into the database.

public void SaveDate(int id, string name, string address)
        {
            try
            {
                SqlConnection con = new SqlConnection("---Your connection string----");
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter("insert into  Table2 (Id,Name,Address) values(" + id + ",'" + name + "','" + address + "')", con);
                con.Open();
                da.SelectCommand.ExecuteNonQuery();
            }
            catch
            {
               //Your exception
            }
        }

Now we will generate the button click event and add the below code.

private void button1_Click(object sender, EventArgs e)
        {
            lblmessage.Text = "Please wait getting date...";
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.RunWorkerAsync();
        }

After this we will add code for DoWork.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //Get data from databse
            DataTable dtProgress = new DataTable();
            dtProgress = GetTable1Data();
            for (int i = 1; i <= dtProgress.Rows.Count; i++)
            {
                Thread.Sleep(100);
                int percents = (i * 100) / dtProgress.Rows.Count;
                //Save data
                int id = Convert.ToInt32(dtProgress.Rows[i - 1]["Id"].ToString());
                string name = dtProgress.Rows[i - 1]["Name"].ToString();
                string address = dtProgress.Rows[i - 1]["Address"].ToString();
                SaveDate(id, name, address);
                backgroundWorker1.ReportProgress(percents, i);
            }        }

In above code I have declare a datatable and access the data from database and then apply loop to show the progress of the date as it get saves. After completing the process it will show 100%.

Now check the complete code of the page.

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

namespace WindowsFormsApplication2
{
    //ProgressBar While Data Transfer In SQL Server Table In Windows Application Using C#.Net
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //Get data from databse
            DataTable dtProgress = new DataTable();
            dtProgress = GetTable1Data();
            for (int i = 1; i <= dtProgress.Rows.Count; i++)
            {
                Thread.Sleep(100);
                int percents = (i * 100) / dtProgress.Rows.Count;
                //Save data
                int id = Convert.ToInt32(dtProgress.Rows[i - 1]["Id"].ToString());
                string name = dtProgress.Rows[i - 1]["Name"].ToString();
                string address = dtProgress.Rows[i - 1]["Address"].ToString();
                SaveDate(id, name, address);
                backgroundWorker1.ReportProgress(percents, i);
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender,
            ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            lblmessage.Text = "Progress: " + e.ProgressPercentage.ToString() + "%";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lblmessage.Text = "Please wait getting date...";
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.RunWorkerAsync();
        }
        public DataTable GetTable1Data()
        {
            SqlConnection con = new SqlConnection("---Your connection string----");
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter("Select Id,Name,Address from Table1", con);
            con.Open();
            da.Fill(dt);

            dt.Clear();
            dt.Columns.Add("CountryName");
            dt.Columns.Add("Population");
            dt.Columns.Add("Code");

            DataRow dataRow1 = dt.NewRow();
            dataRow1["CountryName"] = "India";
            dataRow1["Population"] = "125 Cr";
            dataRow1["Code"] = "IN";
            dt.Rows.Add(dataRow1);

            DataRow dataRow2 = dt.NewRow();
            dataRow2["CountryName"] = "Pakistan";
            dataRow2["Population"] = "50 Cr";
            dataRow2["Code"] = "PK";
            dt.Rows.Add(dataRow2);

            DataRow dataRow3 = dt.NewRow();
            dataRow3["CountryName"] = "United States";
            dataRow3["Population"] = "25 Cr";
            dataRow3["Code"] = "US";
            dt.Rows.Add(dataRow3);

            DataRow dataRow4 = dt.NewRow();
            dataRow4["CountryName"] = "United States";
            dataRow4["Population"] = "25 Cr";
            dataRow4["Code"] = "US";
            dt.Rows.Add(dataRow4);

            DataRow dataRow5 = dt.NewRow();
            dataRow5["CountryName"] = "United States";
            dataRow5["Population"] = "25 Cr";
            dataRow5["Code"] = "US";
            dt.Rows.Add(dataRow5);
            return dt;
        }
        public void SaveDate(int id, string name, string address)
        {
            try
            {
                SqlConnection con = new SqlConnection("---Your connection string----");
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter("insert into  Table2 (Id,Name,Address) values(" + id + ",'" + name + "','" + address + "')", con);
                con.Open();
                da.SelectCommand.ExecuteNonQuery();
            }
            catch
            {
                //Your exception
            }
        }
    }
}

Now we have done run the application to check the output.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top