using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;
namespace App_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Maximum = 100;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int c = 0;
SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=Local_System;Integrated Security=True");
sqlcon.Open();
SqlCommand com = new SqlCommand("SELECT Artist_Name FROM Artists", sqlcon);
SqlDataReader reader;
reader = com.ExecuteReader();
while (reader.Read())
{
// loop thurogh 394 records
comboBox1.Invoke(new MethodInvoker(delegate { comboBox1.Items.Add(reader.GetString(0)); }));
backgroundWorker1.ReportProgress(c += 1);
}
sqlcon.Close();
e.Result = "Loading completed";
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int pres = (int)e.ProgressPercentage;
progressBar1.Value = pres;
// got a exception because i have 394 Artist in my database
// progressbar can hold upto 100
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
string msg = (string)e.Result;
MessageBox.Show(msg);
}
}
}
What my Code will do ?
This will load artist Names into a combobox1
i have use a backgroundworker to keep track on loading bar according to artist loading into combobox
something like this
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int c = 0;
SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=Local_System;Integrated Security=True");
sqlcon.Open();
SqlCommand com = new SqlCommand("SELECT Artist_Name FROM Artists", sqlcon);
SqlDataReader reader;
reader = com.ExecuteReader();
while (reader.Read())
{
// loop thurogh 394 records
comboBox1.Invoke(new MethodInvoker(delegate { comboBox1.Items.Add(reader.GetString(0)); }));
backgroundWorker1.ReportProgress(c += 1);
}
sqlcon.Close();
e.Result = "Loading completed";
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int pres = (int)e.ProgressPercentage;
progressBar1.Value = pres;
// got a exception because i have 394 Artist in my database
// progressbar can hold upto 100
}
i got a exception when exceed the progress bar
i have 380 Artist in my database
progress bar can hold only upto 100
how do i convert this 380 or (n number of artists) to 100% presentage
as a example
progress bar 99% full when the 379 Artist add into combobox1
progress bar 100% full when the 380 (last artist) add into combobox1
if you can come across any mathematical logic please enplane
thanks in advance