Question

I am adding values to a local database on a button click.

This is working fine, but I want to see my changes as soon as click the button.

In order to get the new information that I just created with the button click I have to stop debugging and re-run the program.

Is there an way to update the database and my DataGridView without having to stop and restart the program?

private void addCoachBtn_Click(object sender, EventArgs e)
{
  string ConnectionString = (@"Data Source=|DataDirectory|c:\BaseballDB.sdf");

  try
  {

    SqlCeConnection conn = new SqlCeConnection(ConnectionString);
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = 
      "INSERT INTO Coach " +
      "(FirstName, LastName, Team, Age) " +
      "VALUES " +
      "('" + FirstNametxt.Text + "', '" + LastNametxt.Text + "',  '" + Teamtxt.Text + "','" + Agetxt.Text + "')";
    cmd.ExecuteNonQuery();

    conn.Close();
    this.Close();
  }
  catch (Exception er)
  {
    MessageBox.Show("Not Connected");
    //MainGUI mg = new MainGUI();
    //mg.outputText(er.ToString());
    Console.WriteLine(er);
    MessageBox.Show(er.Message.ToString());
  }

}
Was it helpful?

Solution

Yes, the same code you used to bind the data grid on load, move it to a method and call it after the update is complete. That will re-bind the data grid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top