GBHH
GBHH

Reputation: 213

C# Update MySql Database not Working

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

Answers (4)

csdragonchick
csdragonchick

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

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

You are never calling MyDA.Update() nor you are executing the command.

Upvotes: 1

PiousVenom
PiousVenom

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

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171351

Use UpdateCommand instead of SelectCommand.

See MySqlDataAdapter.UpdateCommand Property

Upvotes: 2

Related Questions