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
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
namespace libPanels
{
public class csCrossEl
{
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()
{
dynPnlCrossEl.Location = new System.Drawing.Point(0, 27);
dynPnlCrossEl.Size = new Size(741, 416);
dynPnlCrossEl.Name = "panelCrossEl";
lblCrossElHeading.Location = new Point(330, 34);
lblCrossElHeading.Size = new Size(125, 13);
lblCrossElHeading.Text = "Cross Elasticity";
lblCrossElHeading.Font = new Font(lblCrossElHeading.Font, FontStyle.Bold);
lblCrossEl.Location = new Point(164, 132);
lblCrossEl.Size = new Size(175, 13);
lblCrossEl.Text = "Cross Elasticity:";
txtCrossEl.Location = new Point(379, 129);
txtCrossEl.Size = new Size(156, 20);
txtCrossEl.Enabled = false;
lblCrossElNew.Location = new Point(159, 252);
lblCrossElNew.Size = new Size(175, 13);
lblCrossElNew.Text = "New Cross Elasticity :";
txtCrossElNew.Location = new Point(379, 249);
txtCrossElNew.Size = new Size(156, 20);
btnCrossElReset.Location = new Point(153, 365);
btnCrossElReset.Size = new Size(93, 23);
btnCrossElReset.Text = "Reset";
btnCrossElReset.Click += new EventHandler(this.btnCrossElReset_Click);
btnCrossElSave.Location = new Point(379, 365);
btnCrossElSave.Size = new Size(93, 23);
btnCrossElSave.Text = "Save";
btnCrossElSave.Click += new EventHandler(btnCrossElSave_Click);
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);
Form.ActiveForm.Controls.Add(dynPnlCrossEl);
}
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
{
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
protected void btnCrossElCancel_Click(object sender, EventArgs e)
{
}
}
}
//and this to display :
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??