I have an application that runs a stored procedure to retrieve all invoices based on Reference ID and populates a data grid (dgResults). All this is working fine. However the databse is large and poorly designed and takes a while to return the results to fill the datagrid. While this is being executed the UI shows (Not Responding). I been playing with BackgroundWorker but got an error when the Stored Procedure completes and it tries to populate the results into the DataGrid.
This is the original code (before BackgroundWorker)
This is the code with BackgroundWorker (not sure if its right, I did not add cancel or progress update to reduce complexity)
When I run the application I get this error
Cross-thread operation not valid: Control 'dgResults' accessed from a thread other than the thread it was created on.
Any help on how to properly use the BackgroundWorker to populate the results of the stored procedure into the datagrid is apreciated
Thanks in advance
RJL
This is the original code (before BackgroundWorker)
Code:
private void btnSearch_Click(object sender, EventArgs e)
{
_ReferenceID = txtRefID.Text;
GetInvoices();
}
public void GetInvoices()
{
DataSet dsInvoice = new DataSet();
DataTable dtInvoice = new DataTable();
SqlCommand cmdInvoice = new SqlCommand();
SqlParameterCollection sqlParameters = (SqlParameterCollection)cmdInvoice.Parameters;
sqlParameters.AddWithValue("@REFERENCE_ID", _ReferenceID);
cmdInvoice.CommandText = "SP_DG_BILLING_INVOICE";
cmdInvoice.CommandType = CommandType.StoredProcedure;
cs.Open();
cmdInvoice.Connection = cs;
cmdInvoice.ExecuteNonQuery();
SqlDataAdapter daInvoice = new SqlDataAdapter();
daInvoice.SelectCommand = cmdInvoice;
daInvoice.Fill(dtInvoice);
dgResults.DataSource = dtInvoice;
cs.Close();
}
This is the code with BackgroundWorker (not sure if its right, I did not add cancel or progress update to reduce complexity)
Code:
private void btnSearch_Click(object sender, EventArgs e)
{
_ReferenceID = txtRefID.Text;
backgroundWorker1.RunWorkerAsync(_ReferenceID);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
GetInvoices();
}
public void GetInvoices()
{
DataSet dsInvoice = new DataSet();
DataTable dtInvoice = new DataTable();
SqlCommand cmdInvoice = new SqlCommand();
SqlParameterCollection sqlParameters = (SqlParameterCollection)cmdInvoice.Parameters;
sqlParameters.AddWithValue("@REFERENCE_ID", _ReferenceID);
cmdInvoice.CommandText = "SP_DG_BILLING_INVOICE";
cmdInvoice.CommandType = CommandType.StoredProcedure;
cs.Open();
cmdInvoice.Connection = cs;
cmdInvoice.ExecuteNonQuery();
SqlDataAdapter daInvoice = new SqlDataAdapter();
daInvoice.SelectCommand = cmdInvoice;
daInvoice.Fill(dtInvoice);
dgResults.DataSource = dtInvoice;
cs.Close();
}
When I run the application I get this error
Cross-thread operation not valid: Control 'dgResults' accessed from a thread other than the thread it was created on.
Any help on how to properly use the BackgroundWorker to populate the results of the stored procedure into the datagrid is apreciated
Thanks in advance
RJL