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.
Some of my previous articles are as follows: Search
File Exist or Present in Directory Windows Application Using C#.Net, Get
List Of All Files Present in Directory In Windows Application Using C#.Net,
Bind
DataGridView With DataTable Using C#.Net In Windows Application, Display
DataGridView Selected Row Values To Second From Using C# In Windows Application,
Bind
DataGridView With Sql Server Table Using C#.Net In Windows Application, How
To Convert Text Into Image In C# In Windows Application.
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.
0 comments:
Please let me know your view