BackgroundWorker and CrystalReports

I have a DBClass that is responsible for executing database queries and SPs.

Here is the code snippet that shows how to use a background thread to get data and generate reports. This is useful when main thread of your applications is doing something else, data data entry and you want reports to be generated in the background.

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;
namespace
myCryst
{
public partial class Form1 : Form
{
DataSet ds;
frmCryst f = new frmCryst();
public Form1()
{
InitializeComponent();
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
bgWorker.ReportProgress(10);
Thread.Sleep(164);
bgWorker.ReportProgress(20);
Thread.Sleep(164);
bgWorker.ReportProgress(30);
Thread.Sleep(164);
ds =
DBClass.ExecQuery("select * from students");
bgWorker.ReportProgress(40);
Thread.Sleep(164);
bgWorker.ReportProgress(50);
Thread.Sleep(164);
bgWorker.ReportProgress(70);
Thread.Sleep(164);
bgWorker.ReportProgress(95);
Thread.Sleep(164);
bgWorker.ReportProgress(100);
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
showCrystalReport();
}
private void button1_Click(object sender, EventArgs e)
{
bgWorker.ProgressChanged +=
new ProgressChangedEventHandler(bgWorker_ProgressChanged);
bgWorker.WorkerReportsProgress =
true;
bgWorker.RunWorkerAsync();
}
private void showCrystalReport()
{
CrystalReport1 c = new CrystalReport1();
Reportds d = new Reportds();
d.Tables[0].Merge(ds.Tables[
"tab"]);
frmCryst f = new frmCryst();
c.SetDataSource(d);
f.crystRep.ReportSource = c;
f.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}

Up Next
    Ebook Download
    View all
    Learn
    View all