Solved

Help with C# mult threaded sql binding

Posted on 2011-02-23
1
486 Views
Last Modified: 2012-05-11
My goal with this program is create a class that accepts three arguments (sql statement, datagridview, db connection) and then executes the SQL command on another thread. I am having an issue when it comes to invoking the data grid. Shown below is my code.

namespace multThreadDB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        NpgsqlConnection conn = new NpgsqlConnection();
       
        private void button1_Click(object sender, EventArgs e)
        {
            db1 d = new db1("select * from meter;", dataGridView1, conn);
            new Thread(new ThreadStart(d.fillGrid)).Start();
        }
    }

    public class db1
    {
        string sql;
        DataGridView grid;
        NpgsqlConnection conn;
        DataSet ds = new DataSet();
       

        public db1(string sql, DataGridView grid, NpgsqlConnection conn)
        {
            this.grid = grid;
            this.sql = sql;
            this.conn = conn;
        }

        public void fillGrid()
        {
            conn.Open();
            NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(sql, conn);
            adapter.Fill(ds);
            SetResult(ds);
            conn.Close();
        }

        delegate void SetDataSetDelegate(DataSet parameter);

        void SetResult(DataSet ds)
        {
            try
            {
                if (!InvokeRequired)
                    grid.DataSource = ds.Tables[0];
                else
                    Invoke(new SetDataSetDelegate(SetResult), new object[] { ds });
            }
            catch { }
        }
    }
}

I receiving errors on the invoke lines. It seems the error is caused because i am invoking a control from another class? How can I fix this?

Thanks in advance!
0
Comment
Question by:j4jones
1 Comment
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 34962869
UI controls can only be accessed from the UI Thread.  If you try to access it from a worker thread an exception is thrown.  I think that's what's happening.

Instead of the Thread class I suggest that you use the BackgroundWorker class, which already provide the necessary support.

More about BackgroundWorker:
http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question