Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is code under button in form
C#
con = new SqlConnection(str);
            con.Open();
            string combined = "select code_customer,insurance_type,branch_name,name,address,insurance_cost,record_date  from customers,branches,insurance_type where branches.code_branch=customers.code_branch and insurance_type.code_type=customers.code_type";
            dset = new DataSet();
            da = new SqlDataAdapter(combined, con);
            da.Fill(dset, "customers");
            builder = new SqlCommandBuilder(da);            
            dataGridView1.DataSource = dset.Tables["customers"];
            comboBox1.DataSource = dset.Tables["customers"];
            comboBox1.DisplayMember = "insurance_type";
            comboBox2.DataSource = dset.Tables["customers"];
            comboBox2.DisplayMember = "branch_name";
            textBox1.DataBindings.Add("Text",dset.Tables["customers"], "name");
            textBox2.DataBindings.Add("Text",dset.Tables["customers"], "address");
            textBox3.DataBindings.Add("Text",dset.Tables["customers"], "insurance_cost");
            dateTimePicker1.DataBindings.Add("value", dset.Tables["customers"], "record_date");
Posted
Updated 4-Dec-11 3:48am
v2

1 solution

It is because you are binding your controls on Button Click Event.When you click Button then your Controls are bind with your DataSet Tables and when you press this Button Second time your code will try to Bind this control with DataSet again which will give you error.Try to write the code you used for binding the Controls in Form_Load Event then it will bind controls when form gets loaded.

See this link to know more about DataBinding :
http://msdn.microsoft.com/en-us/library/ms752347.aspx[^]
http://msdn.microsoft.com/en-us/library/cc278072(v=vs.95).aspx[^]

So do one thing Add this code before your code :
C#
textBox1.DataBindings.Clear();
textBox2.DataBindings.Clear();
textBox3.DataBindings.Clear();
dateTimePicker1.DataBindings.Clear();

It will clear your old bindings when your press Button.
I hope it will help you. :)
 
Share this answer
 
v2
Comments
mhassan083 4-Dec-11 10:07am    
thanks my dear, but i need this in button not in form_load what i do??
Manoj K Bhoir 4-Dec-11 11:18am    
See updated Solution!

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