Solved

Help with C# mult threaded sql binding

Posted on 2011-02-23
1
483 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now