I have searched, but nothing happens, I just starting using C#.NET and I a have a textbox on a form. I retrieve some data from the database and I display to a textbox through a combobox that indicate the section I want to display (I already do this!),but when I try to update nothing works, I click my button to update the access database(Access 2007) and nothing happens, the user just changes something and the button has to update the acces database, I hope you can help me 😀
this is my code so far:
String textTobeUpdated = textBox3.Text;
String thing = comboBox2.Text;
using (var conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=MyDataBase.accdb"))
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "UPDATE Section SET content = [@content] WHERE name= thing";
cmd.Parameters.AddWithValue("@content",content);
conn.Open();
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected == 1)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show(string.Format("{0} Rows Affected", rowsAffected));
}
this code display a message that tells “Unhandled exception. If click in continue, the application will omit this error and intent continue. If click in exit, the application will close immediately” The are no especified value for some of the required parameters.
Looks like you execute the query twice (two areas where you call
cmd.ExecuteNonQuery();)Remove the first one and leave the line that says
This line will perform the update, then count how many rows were updated. This count is then used to display a message to the user.
You’re also not setting the
contentvariable with a value, (I guess it should be the textbox value), and the update query itself seems a little odd, you haven’t parameterised the ‘thing’ you’re updating, so I guess it should be: