Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# BackgroundWorker
//button1 is start searching
private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            OleDbCom = new OleDbCommand("SELECT COUNT(VOSO_Id) FROM " + FrVosolSoratjalase.Vosol_table_2, Program._Connect);
            MaxCountRowsExist = Classes.AbstractLayer.ReturnCountValue(OleDbCom);
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); return; }
 
        if (!backgroundWorker1.IsBusy)
        {
            progressBar1.Visible = true;
            textBox1.Enabled = button1.Enabled = button3.Enabled = false;
            progressBar1.Value = 0;
            backgroundWorker1.RunWorkerAsync();
            button2.Enabled = true;
        }
    }
//button2 is cancel
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        NewDatatable("ResultSearch");
        BackgroundWorker worker = sender as BackgroundWorker;
        #region Method 2
        int i = 0;
 
        if (worker.CancellationPending)
        {
            e.Cancel = true;
            return;
        }
        else
        {
            try
            {
                OleDbComSearching = new OleDbCommand("SELECT * FROM " + FrVosolSoratjalase.Vosol_table_1 + " INNER JOIN " + FrVosolSoratjalase.Vosol_table_2 + " ON " + FrVosolSoratjalase.Vosol_table_1 + ".VOSO_Id = " + FrVosolSoratjalase.Vosol_table_2 + ".VOSO_Id;", Program._Connect);
                OleDbComSearching.Connection.Open();
                OleDbDataRead = OleDbComSearching.ExecuteReader();
                while (OleDbDataRead.Read())
                {
                    if (textBox1.Text.Trim() == OleDbDataRead[6].ToString() | textBox1.Text.Trim() == OleDbDataRead[7].ToString())
                    {
                        DataRow dtrow = FrVosolSoratjalase.DataSetVosol.Tables["ResultSearch"].NewRow();
                        dtrow[0] = OleDbDataRead[0].ToString();
                        dtrow[1] = OleDbDataRead[1].ToString();
                        dtrow[2] = OleDbDataRead[2].ToString();
                        dtrow[3] = OleDbDataRead[3].ToString();
                        dtrow[4] = OleDbDataRead[4].ToString();
                        dtrow[5] = OleDbDataRead[5].ToString();
                        dtrow[6] = OleDbDataRead[6].ToString();
                        dtrow[7] = OleDbDataRead[7].ToString();
                        dtrow[8] = OleDbDataRead[8].ToString();
                        dtrow[9] = OleDbDataRead[9].ToString();
                        dtrow[10] = OleDbDataRead[10].ToString();
                        dtrow[11] = OleDbDataRead[11].ToString();
                        dtrow[12] = OleDbDataRead[12].ToString();
                        dtrow[13] = OleDbDataRead[13].ToString();
                        FrVosolSoratjalase.DataSetVosol.Tables["ResultSearch"].Rows.Add(dtrow);
                    }
                    i++;
                    worker.ReportProgress((int)((double)i / MaxCountRowsExist * 100));
                }
            }
            catch (InvalidOperationException) { MessageBox.Show("InvalidOperationException"); }
            catch (NullReferenceException) { MessageBox.Show("NullReferenceException"); }
            finally
            {
                OleDbDataRead.Close();
                OleDbCom.Connection.Close();
            }
        }
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            progressBar1.Visible = false;
            textBox2.Text = "";
            textBox1.Enabled = button1.Enabled = button3.Enabled = true;
            button2.Enabled = false;
            MessageBox.Show(e.Error.Message);
        }
        else if (e.Cancelled)
        {
            dataGridView1.DataSource = FrVosolSoratjalase.DataSetVosol.Tables["ResultSearch"];
            textBox2.Text = dataGridView1.RowCount.ToString();
            textBox2.Text = "";
        }
        else
        {
            dataGridView1.DataSource = FrVosolSoratjalase.DataSetVosol.Tables["ResultSearch"];
            textBox2.Text = dataGridView1.RowCount.ToString();
            progressBar1.Visible = false;
            textBox1.Enabled = button1.Enabled = button3.Enabled = true;
            button2.Enabled = false;
        }
    }
this codes have 2 exceptions
 
1-InvalidoperationException [No data exists for the row/column] on :
if (textBox1.Text.Trim() == OleDbDataRead[6].ToString() | textBox1.Text.Trim() == OleDbDataRead[7].ToString())
2-NullReferencException [Object reference not set to an instance of an object] on :
while (OleDbDataRead.Read())
CAN YOU HELP ME ?
Posted 17-Aug-12 8:57am
Edited 17-Aug-12 9:04am
v2
Comments
obp42 at 17-Aug-12 15:53pm
   
Start by changing
if (textBox1.Text.Trim() == OleDbDataRead[6].ToString() | textBox1.Text.Trim() == OleDbDataRead[7].ToString())
to
if (textBox1.Text.Trim() == OleDbDataRead[6].ToString() || textBox1.Text.Trim() == OleDbDataRead[7].ToString())
Wes Aday at 17-Aug-12 15:56pm
   
Good catch
ahmadamiri at 17-Aug-12 16:21pm
   
Is ineffective....
i like use mode blocks of progress bar and edit data when read data of database
obp42 at 17-Aug-12 16:45pm
   
Have you debugged to make sure you are actually running the query you intend to?
ahmadamiri at 17-Aug-12 17:04pm
   
yes,it`s run query
Even results also show
I check for exist users in application ,the use of a timer and sql command
this 2 exception show sometime that i do not know reason

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 17 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100