none
Cannot update changes to DB RRS feed

  • Question

  • Hi all,

    The follow code make DataGridView changes only, but cannot commit to database (not exception error, just don't do it).

    By the same way, If I just change some value for the existing record, that's work, I wondered.

                MySqlCommand mySqlCommand = new MySqlCommand("select * from payment", GetConnection());
                MySqlDataAdapter DataAdapter = new MySqlDataAdapter(mySqlCommand);
                MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(DataAdapter);
                DataTable DataTable = new DataTable();
                DataAdapter.Fill(DataTable);

                BindingSource BindingSource = new BindingSource();
                BindingSource.DataSource = DataTable;

                DataRow newRow = DataTable.NewRow();

                newRow.BeginEdit();
                newRow["cust_name"] = "abc";
                newRow["amount"] = 500;
                newRow.EndEdit();

                DataTable.Rows.Add(newRow);
                newRow.AcceptChanges();
                DataTable.GetChanges();
                DataTable.AcceptChanges();

                DataAdapter.Update(DataTable);


    Hope someone can help. Thank you very much.

    Best regards,
    Diviner.
    Saturday, December 27, 2008 7:29 PM

Answers

  • Because you call AcceptChanges you indicate that the values are up-to-date. Therefore the changes will not be populated to the database.

    Please change your code to the following. That should work (changed code is highlighted).

                MySqlCommand mySqlCommand = new MySqlCommand("select * from payment", GetConnection());
                MySqlDataAdapter DataAdapter = new MySqlDataAdapter(mySqlCommand);
                MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(DataAdapter);
                DataTable DataTable = new DataTable();
                DataAdapter.Fill(DataTable);

                BindingSource BindingSource = new BindingSource();
                BindingSource.DataSource = DataTable;

                DataRow newRow = DataTable.NewRow();
                newRow["cust_name"] = "abc";
                newRow["amount"] = 500;
                DataTable.Rows.Add(newRow);
                DataAdapter.Update(DataTable);

    Ewald - Please remember to mark the replies as answers if they help.
    • Marked as answer by Diviner Chan Saturday, December 27, 2008 8:39 PM
    Saturday, December 27, 2008 8:25 PM

All replies

  • Because you call AcceptChanges you indicate that the values are up-to-date. Therefore the changes will not be populated to the database.

    Please change your code to the following. That should work (changed code is highlighted).

                MySqlCommand mySqlCommand = new MySqlCommand("select * from payment", GetConnection());
                MySqlDataAdapter DataAdapter = new MySqlDataAdapter(mySqlCommand);
                MySqlCommandBuilder commandBuilder = new MySqlCommandBuilder(DataAdapter);
                DataTable DataTable = new DataTable();
                DataAdapter.Fill(DataTable);

                BindingSource BindingSource = new BindingSource();
                BindingSource.DataSource = DataTable;

                DataRow newRow = DataTable.NewRow();
                newRow["cust_name"] = "abc";
                newRow["amount"] = 500;
                DataTable.Rows.Add(newRow);
                DataAdapter.Update(DataTable);

    Ewald - Please remember to mark the replies as answers if they help.
    • Marked as answer by Diviner Chan Saturday, December 27, 2008 8:39 PM
    Saturday, December 27, 2008 8:25 PM
  • Ewald Hofman,

    Yes. It works. You save me a lot.

    Thank you very much.

    Best regards,
    Diviner.
    Saturday, December 27, 2008 8:39 PM