Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error description:
An unhandled exception of type "System.InvalidOperationException" occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control" accessed from a thread other than the thread it was created on.

i have my application as:

//-------A class library that contains data from sql server

C#
namespace libDataStore
{
    public class csGetData
    {


        static string strCon =                ConfigurationManager.ConnectionStrings["MyCon"].ToString();
        SqlConnection con = new SqlConnection(strCon);
        
        public Single crossEl {get; set;}

        public void getCrossEl()
        {           
            SqlConnection conGetCrossEl = new SqlConnection(strCon);
            conGetCrossEl.Close();
            conGetCrossEl.Open();
            SqlCommand cmd1 = new SqlCommand("usp_getCrossEl", conGetCrossEl);
            cmd1.CommandType = CommandType.StoredProcedure;
            try
            {
                SqlDataReader dr = cmd1.ExecuteReader();
                dr.Read(); 
                crossEl = 0;
                crossEl = Convert.ToSingle(dr[0].ToString());                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());                
            }            
            conGetCrossEl.Close();
            conGetCrossEl.Dispose();
        }
         

    }

}


//------------- Another Class library which contain panels


C#
namespace libPanels
{   
    public class csCrossEl   //from ClassLibrary libPanels

    {                       
        Label lblCrossElHeading = new Label();
        Label lblCrossEl = new Label();
        TextBox txtCrossEl = new TextBox();
        Label lblCrossElNew = new Label();
        TextBox txtCrossElNew = new TextBox();
        Button btnCrossElReset = new Button();
        Button btnCrossElSave = new Button();
        Button btnCrossElCancel = new Button();

        FlowLayoutPanel dynPnlCrossEl = new FlowLayoutPanel();
        public void createPanelCrossEl()
        {
            //Panel dynamicPanelEl = new Panel();
            dynPnlCrossEl.Location = new System.Drawing.Point(0, 27);
            dynPnlCrossEl.Size = new Size(741, 416);
            dynPnlCrossEl.Name = "panelCrossEl";

            //Label lblCrossElHeading = new Label();
            lblCrossElHeading.Location = new Point(330, 34);
            lblCrossElHeading.Size = new Size(125, 13);
            lblCrossElHeading.Text = "Cross Elasticity";
            lblCrossElHeading.Font = new Font(lblCrossElHeading.Font, FontStyle.Bold);

            //Label lblCrossEl = new Label();
            lblCrossEl.Location = new Point(164, 132);
            lblCrossEl.Size = new Size(175, 13);
            lblCrossEl.Text = "Cross Elasticity:";

            //TextBox txtCrossEl = new TextBox();
            txtCrossEl.Location = new Point(379, 129);
            txtCrossEl.Size = new Size(156, 20);
            txtCrossEl.Enabled = false;

            //Label lblCrossElNew = new Label();
            lblCrossElNew.Location = new Point(159, 252);
            lblCrossElNew.Size = new Size(175, 13);
            lblCrossElNew.Text = "New Cross Elasticity :";

            //TextBox txtCrossElNew = new TextBox();
            txtCrossElNew.Location = new Point(379, 249);
            txtCrossElNew.Size = new Size(156, 20);

            //Button btnCrossElReset = new Button();
            btnCrossElReset.Location = new Point(153, 365);
            btnCrossElReset.Size = new Size(93, 23);
            btnCrossElReset.Text = "Reset";
            btnCrossElReset.Click += new EventHandler(this.btnCrossElReset_Click);

            //Button btnCrossElSave = new Button();
            btnCrossElSave.Location = new Point(379, 365);
            btnCrossElSave.Size = new Size(93, 23);
            btnCrossElSave.Text = "Save";
            btnCrossElSave.Click += new EventHandler(btnCrossElSave_Click);

            //Button btnCrossElCancel = new Button();
            btnCrossElCancel.Location = new Point(603, 365);
            btnCrossElCancel.Size = new Size(93, 23);
            btnCrossElCancel.Text = "Cancel";
            btnCrossElCancel.Click += new EventHandler(btnCrossElCancel_Click);

            
            dynPnlCrossEl.Controls.Add(lblCrossElHeading);
            dynPnlCrossEl.Controls.Add(lblCrossEl);
            dynPnlCrossEl.Controls.Add(txtCrossEl);
            dynPnlCrossEl.Controls.Add(lblCrossEl);
            dynPnlCrossEl.Controls.Add(txtCrossEl);
            dynPnlCrossEl.Controls.Add(btnCrossElReset);
            dynPnlCrossEl.Controls.Add(btnCrossElSave);
            dynPnlCrossEl.Controls.Add(btnCrossElCancel);

            //this.Controls.Add(dynPnlCrossEl);
            Form.ActiveForm.Controls.Add(dynPnlCrossEl);    //This line causes stated error

        }

        public void addFunctionality()
        {
            csGetData obdata = new csGetData();
            obdata.getCrossEl();
            txtCrossEl.Text = Convert.ToString(obdata.crossEl);

        }

        protected void btnCrossElReset_Click(object sender, EventArgs e)
        {
            txtCrossEl.Clear();
        }

        protected void btnCrossElSave_Click(object sender, EventArgs e)
        {
            if (txtCrossEl.Text == "")
            { MessageBox.Show("Please fill Proper Values.."); }
            else
            {
                try
                {
                    //do_something
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }

        }

        protected void btnCrossElCancel_Click(object sender, EventArgs e)
        {
            //HideAllPanel();
            //dynamicPanelWelcome.Visible = true;
        }
    }
}



//and this to display :

C#
namespace MyProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();          
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            csCrossEl obCrossEl = new csCrossEl();            
            obCrossEl.addFunctionality();
            obCrossEl.createPanelCrossEl();
        }
    }
}


What I have tried:

I have this application with two more class libraries, in which one hold data and second create panel
t
i think problem arise coz of panel Class tends to display data on active form
active form is one thread and class libraries are other two threads?
how to solve it??
Posted
Updated 3-Jul-16 19:15pm
v2
Comments
Member 11712753 1-Jul-16 4:46am    
Which line causes the exception exactly??
Satpal Lakhera 4-Jul-16 1:15am    
Form.ActiveForm.Controls.Add(dynPnlCrossEl);

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900