Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BackgroundWorker Execute Stored Procedure and Fill Datagrid with Results

Status
Not open for further replies.

RJL1

Technical User
Oct 3, 2002
228
US
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)

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
 
I know you're asking about the Background worker, but have you thought about paging your results with LINQ and only returning 25-100 rows at a time?

You're going to need to create a delegate to handle the updating part of the work.

You've got questions and source code. We want both!
Here at tek tips, we provide a hand up, not a hand out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top