Solved

Help with C# mult threaded sql binding

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# DataTable (in memory) vs SQL Lookups 4 75
How we can add Dten600.dll in GAC in c#. 18 55
What!!!??? 5 61
VS C# Creating a library of functions 3 28
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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