Hello,
I have the following problem.I have a form with some textboxes(name, street, address etc...)that are binded to the same datatable.On that form there is also a button and when this button is clicked the textboxes of the street and place are binded to another datatable to choose another street and place.I can save the new values of street and place by clicking on a save button.These values are saved in the previous datatable.When the save button is clicked the street and place textboxes need to be binded back to the previous datatable.When i bind it back to the previous datatable the textboxes street and place are not updated directly.Only when i start the application again then the data in the textboxes are updated.I have also a textbox binded to another datatable and this textbox is not binded to another datatable and when i make changes the data in this textbox is updated instantly.Here is my code:
namespace WindowsFormsApplication1{ public partial class broekx : Form { public SqlConnection connection; public broekx() { InitializeComponent(); }
private void button2_Click(object sender, EventArgs e) { try { string connectString = "Persist Security Info=False;User ID=gerry;Password=3600gerry;Initial Catalog=BROEKX_038951_311;Data Source=webdata99.broekx.be"; connection = new SqlConnection(connectString); connection.Open(); } catch (SqlException sqlex) { MessageBox.Show(sqlex.Message, "Error"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } this.Hide(); Form1 frm1 = new Form1(); frm1.Show(); }
private void toolStripButton1_Click(object sender, EventArgs e) { this.Close(); }
}}
namespace WindowsFormsApplication1{ public partial class Form1 : Form { public DataSet ds = new DataSet(); public DataTable dt = new DataTable(); public BindingSource binding1 = new BindingSource(); public DataSet data2 = new DataSet(); public DataTable dt2 = new DataTable(); public SqlDataAdapter dataAdapter1; public SqlDataAdapter dataAdapter2; public BindingSource binding2 = new BindingSource();
public Form1() { InitializeComponent(); FillPerson(); FillPostcode(); }
public void FillPerson() { string sql = "Select * From [038951_311_PERSON_PERSON]";
dataAdapter1 = new SqlDataAdapter(sql, br.connection); dataAdapter1.Fill(ds, "038951_311_PERSON_PERSON"); dt = ds.Tables["038951_311_PERSON_PERSON"]; binding1.DataSource = dt; } public void FillPostcode() { string sql2 = "Select * From [038951_311_PERSON_POSTCOD]";
dataAdapter2 = new SqlDataAdapter(sql2, br.connection); dataAdapter2.Fill(data2, "038951_311_PERSON_POSTCOD"); dt2 = data2.Tables["038951_311_PERSON_POSTCOD"]; binding2.DataSource = dt2; } }}
namespace WindowsFormsApplication1{ public partial class Personeel : Form { Form1 frm1 = (Form1)Application.OpenForms["Form1"]; broekx br = (broekx)Application.OpenForms["broekx"]; public Personeel() { InitializeComponent(); bindingNavigator2.BindingSource = frm1.binding1; }
private void VulTextboxen() { this.textBox11.DataBindings.Add(new Binding("Text", frm1.binding1, "POSTCODE", true)); this.textBox12.DataBindings.Add(new Binding("Text", frm1.binding1, "GEMEENTE", true));
}private void Personeel_Load(object sender, EventArgs e) { VulTextboxen(); }
private void button1_Click(object sender, EventArgs e) { Zoekgemeente gem = new Zoekgemeente(); gem.Show(); keuzewijzig = "verblijfplaats"; this.textBox13.DataBindings.Clear(); this.textBox14.DataBindings.Clear(); this.textBox13.DataBindings.Add(new Binding("Text", frm1.binding2, "GEMEENTE", true)); this.textBox14.DataBindings.Add(new Binding("Text", frm1.binding2, "POSTCODE", true)); }
private void toolStripButton6_Click(object sender, EventArgs e) {
switch (keuzewijzig) { case "woonplaats": wijzigPlaats(br.connection, frm1.dataAdapter1, frm1.ds); this.textBox11.DataBindings.Clear(); this.textBox12.DataBindings.Clear(); this.textBox11.DataBindings.Add(new Binding("Text", frm1.binding1,"POSTCODE", true)); this.textBox12.DataBindings.Add(new Binding("Text", frm1.binding1, "GEMEENTE", true)); bindingNavigator2.Visible = true; break;
case "verblijfplaats": wijzigVerblijfPlaats(br.connection, frm1.dataAdapter1, frm1.ds); bindingNavigator2.Visible = true; break;
} }
private void wijzigPlaats(SqlConnection mySqlConnectionwijzigplaats, SqlDataAdapter mySqlDataAdapterwijzigplaats, DataSet myDataSetwijzigplaats) { string updateString = "UPDATE [038951_311_PERSON_PERSON] " + "SET " + " POSTCODE = @postcode, " + " GEMEENTE = @gemeente " + "WHERE ID = @id";
SqlCommand mySqlCommand = mySqlConnectionwijzigplaats.CreateCommand(); mySqlCommand.CommandText = updateString;
mySqlCommand.Parameters.Add("@postcode", SqlDbType.NVarChar, 10); mySqlCommand.Parameters.Add("@gemeente", SqlDbType.NVarChar, 30); mySqlCommand.Parameters.Add("@id", SqlDbType.Int, 5);
mySqlCommand.Parameters["@postcode"].Value = textBox11.Text; mySqlCommand.Parameters["@gemeente"].Value = textBox12.Text; mySqlCommand.Parameters["@id"].Value = Convert.ToInt32(bindingNavigatorPositionItem2.ToString());
mySqlCommand.ExecuteNonQuery();
}
private void wijzigVerblijfPlaats(SqlConnection mySqlConnection, SqlDataAdapter mySqlDataAdapter, DataSet myDataSet) { string updateString = "UPDATE [038951_311_PERSON_PERSON] " + "SET " + " POSTCDVP = @postcdvp, " + " VERBLIJFP = @verblijfp " + "WHERE ID = @id";
SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); mySqlCommand.CommandText = updateString;
mySqlCommand.Parameters.Add("@postcdvp", SqlDbType.NVarChar, 10); mySqlCommand.Parameters.Add("@verblijfp", SqlDbType.NVarChar, 30); mySqlCommand.Parameters.Add("@id", SqlDbType.Int, 5);
mySqlCommand.Parameters["@postcdvp"].Value = textBox14.Text; mySqlCommand.Parameters["@verblijfp"].Value = textBox13.Text; mySqlCommand.Parameters["@id"].Value = Convert.ToInt32(bindingNavigatorPositionItem2.ToString());