Reputation: 213
I am trying to update my MySql database but it won't work. I have inserting and selecting working fine. Not sure if it is my syntax on the query or what.
string id = invoicenumb.Text;
string mysqlIns1 = "UPDATE invoices SET Status = '" +
comboBox1.SelectedItem.ToString() + "' WHERE id = '" +
Convert.ToInt16(id) + "'";
try
{
MySqlConnection mysqlCon = new MySqlConnection(mysqlProv);
mysqlCon.Open();
MySqlDataAdapter MyDA = new MySqlDataAdapter();
MyDA.SelectCommand = new MySqlCommand(mysqlIns1, mysqlCon);
MessageBox.Show("Success!");
mysqlCon.Close();
}
catch
{
MessageBox.Show("Error Occured Please Try Again");
}
Upvotes: 0
Views: 1310
Reputation: 51
Try MyDA.UpdateCommand = new MySqlCommand(mysqlIns1, mysqlCon);
Instead of MyDA.SelectCommand = new MySqlCommand(mysqlIns1, mysqlCon);
Also, you need to call MyDA.Update() as is listed by Nick.
Upvotes: 1
Reputation: 8656
You are never calling MyDA.Update()
nor you are executing the command.
Upvotes: 1
Reputation: 6908
Try using ExecuteNonQuery()
and MySqlCommand
.
try
{
MySqlConnection mysqlCon = new MySqlConnection(mysqlProv);
mysqlCon.Open();
MySqlCommand MyDA = new MySqlCommand(mysqlIns1, mysqlCon);
MyDA.ExecuteNonQuery();
MessageBox.Show("Success!");
mysqlCon.Close();
}
catch
{
MessageBox.Show("Error Occured Please Try Again");
}
This should work.
Upvotes: 1
Reputation: 171351
Use UpdateCommand
instead of SelectCommand
.
See MySqlDataAdapter.UpdateCommand Property
Upvotes: 2