Link to home
Start Free TrialLog in
Avatar of HarjeetSingh
HarjeetSingh

asked on

Cross-thread Problem

Hi

My Requirement is something like timeControl. What I want my application constantly look at server (at certain interval) and as soon as new record is inserted in Database it Show back in DataGridView. Pl help me
 
        static string conn = "Data Source=192.168.0.18;Initial Catalog=hotel;User ID=sa";
        SqlConnection cn = new SqlConnection(conn);
        SqlDataAdapter ad;
        SqlCommand cmd;
        DataSet ds;
        Thread th;
        public frmDownload()
        {
            InitializeComponent();
            //Control.CheckForIllegalCrossThreadCalls = false;
        }
        private void frmDownload_Load(object sender, EventArgs e)
        {
            cn.Open();
            try
            {
                th = new Thread(new ThreadStart(GetRemoteData1));
                th.Priority = ThreadPriority.Highest;
                th.Start();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message, "Unable to Connect Remote Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        void GetRemoteData1()    
        {
                cmd = new SqlCommand("SpBookingGuestDetail", cn);
                ad = new SqlDataAdapter(cmd);
                ds = new DataSet();
                ad.Fill(ds);
                DataGridRemote.DataSource = ds.Tables[0];
//////There Error Raises ---- Cross-thread Windows Form Control
                Thread.Sleep(1000);
        }
}

Pl Help
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial