0

I have a problem with BackgroundWorker! When I get data from Database and try to transfer data to RunWorkerCompleted, I get an error:

System.NullReferenceException The reference to the object does not indicate an instance of the object e was null.

the problem arises in e.Result = dataTable;

private void BackgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)   
{
        string Refresh = "dbo.GetRenderedService '" + dateTimePicker2.Value.Date.ToString() + "','" + dateTimePicker4.Value.Date.ToString() + "'";
        DataTable dataTable = DbConnection.DBConnect(Refresh);
        int i = 1;
        try
        {enter code here
            foreach (DataRow dr in dataTable.Rows)
            {
                backgroundWorker1.ReportProgress(i);
                Thread.Sleep(100);
                i++;
            }
            e.Result = dataTable;
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

private void BackgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
    progBar.Value = e.ProgressPercentage;
}

private void BackgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
        source.DataSource = e.Result;
        dataGridView1.DataSource = source;
        dataGridView1.Columns[0].Visible = false;
        dataGridView1.Columns[1].Visible = false;
        dataGridView1.Columns[2].Visible = false;
        dataGridView1.Columns[3].Visible = false;
        dataGridView1.Columns[4].Visible = false;
        dataGridView1.Columns[5].Visible = false;
        dataGridView1.Columns[6].Visible = false;
}

This is the class for fetching data from SQL Server.

class DbConnection
{
    public static string connectionString = "Data Source=POTITPC-01\\PLMLOCAL;Initial Catalog=Batys;User ID=sa;Password=!sql123;";
    public static DataTable DBConnect(string query)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlDataAdapter da = new SqlDataAdapter(query, conn);
        DataTable dataTable = new DataTable();

        try
        {
            conn.Open();
            da.Fill(dataTable);
            conn.Close();
            conn.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return dataTable;
    }
  • If you put a breakpoint on the line containing `e.Result = dataTable;` what is the value of `dataTable`? Does `e.Result` get its value set if you step over it (in the debugger)? – Minijack Sep 25 '19 at 05:57
  • It would be much appreciated if you could show all code relating to your problem so that we can help you more (i.e. the code that calls the background worker) – Minijack Sep 25 '19 at 05:58
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sinatr Sep 25 '19 at 07:23

0 Answers0