0

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());
  }

}
1
  • 1
    To output the error message to MainGUI, delete the line MainGUI mg = new MainGUI(); and replace mg.outputText(er.ToString()); with this.outputText(er.ToString()); Commented Dec 2, 2013 at 21:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.