Solved

Help with C# mult threaded sql binding

Posted on 2011-02-23
1
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

623 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