×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

BackgroundWorker Execute Stored Procedure and Fill Datagrid with Results

BackgroundWorker Execute Stored Procedure and Fill Datagrid with Results

BackgroundWorker Execute Stored Procedure and Fill Datagrid with Results

(OP)
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

RE: BackgroundWorker Execute Stored Procedure and Fill Datagrid with Results

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close