I searched and checked but did not see what I need. I am using a data adapter's UpdateCommand's ExecuteNonQuery to update a local Access database. When I run the code, the ExecuteNonQuery does not appear to finish before the next line of code is executed and therefore, the updated database is not reflected in my grid or my text boxes. If I put a messagbox right after the ExecuteNonQuery, then when I hit ok in the messagebox, the rest of the code is executed and the updated database is reflected in my grid and text boxes. Or if I step through the code in debug without the messagebox to hault execution, the update to the database is reflected on my form. But if I just run the code without something after the query to slow it down or stop it, the update is not shown in my controls.
OleDbConnection connection = new OleDbConnection(connString);
string sql = "Update Table1 set Country = '" + txtNewText.Text + "' where SomeNum =
1111";
connection.Open();
dAdapter.UpdateCommand = connection.CreateCommand();
dAdapter.UpdateCommand.CommandText = sql;
dAdapter.UpdateCommand.ExecuteNonQuery();
//MessageBox.Show("Pause");
dTable.Clear(); //Clear or it will add everything to what was there.
dAdapter.Fill(dTable);
row = dTable.Rows[0];
txtCompany.Text = row["Name"].ToString();
txtGenre.Text = row["Job"].ToString();
txtId.Text = row["Id"].ToString();
txtSomeNum.Text = row["SomeNum"].ToString();
txtCountry.Text = row["Country"].ToString();
I'm assuming that the update query is not finishing before I re-fill the data table. Can someone please help me with this. I looked for something to show the status of the update query but did not see what I need. What am I missing here???
Thank you!
VH
10/28/14 Last night I added dAdapter.UpdateCommand.ExecuteNonQuery() right after dAdapter.UpdateCommand.ExecuteNonQuery(); then everything worked. I was not sure if this was just something that took enough time to allow the query to finish and therefore causing the update to display, or if it was just some fluke "band-aide" that worked. I then replaced that with connection.Close(); and that worked as well. Again...I don't know if closing the connection caused the query to finish and the connection to close before moving to the next line of code, or if this was also just "fudging" it to work. I have seen other people having similar problems in my searching for answers but I have not seen any one explain what the problem is and why this happens or the most appropriate solution.