0

I am trying to make a button that updates account nickname using the account number but Im getting an error.

enter image description here

  private void change_nickname_Click(object sender, EventArgs e)
    {
        try
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string query = "update customers set [CustomerCode]='" + customercode.Text + "',[CustomerName]='" + customername.Text + "',[Address]='" + customeraddress.Text + "',[PhoneNumber]='" + customerphone.Text + "',[Account]='" + Account + "',[AccountNickname]='" + accountnickname.Text + "',[Overdraft]='" + overdraft.Text + "' where AccountNumber=" + accountnumber.Text + "";
            MessageBox.Show(query);
            command.CommandText = query;

            command.ExecuteNonQuery();
            MessageBox.Show("Data updated successfuly!");
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex);
        }
        connection.Close();
    }
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • What is the error message? – Lewis Taylor Dec 05 '15 at 17:03
  • http://i.stack.imgur.com/End36.png – Emil Ivanov Dec 05 '15 at 17:05
  • Which RDBMS is this for? Please add a tag to specify whether you're using `mysql`, `postgresql`, `sql-server`, `oracle` or `db2` - or something else entirely. – marc_s Dec 05 '15 at 17:06
  • Oh its an access database. OleDb. – Emil Ivanov Dec 05 '15 at 17:07
  • 2
    you should use Parameterized query's doing it dynamically you will get yourself all mixed up trying to match `'` and double `" "` quotes I would suggest wrapping your code around `using statment` as well – MethodMan Dec 05 '15 at 17:15
  • One or more of the parameters to your update statement is null (or empty) while the database is expecting a value. Check the values of your text fields. Also you just have `Account` and not `account.Text` for your `Account` field. Is this deliberate? – ChrisF Dec 05 '15 at 17:18
  • Yes it it because Account is a variable that i can later use for something else. – Emil Ivanov Dec 05 '15 at 20:16

1 Answers1

0

Yeah the problem was solved guys thank for all the help. The problem was here.

where AccountNumber=" + accountnumber.Text + "";
        MessageBox.Show(query);

I changed this to

where AccountNumber='" + accountnumber.Text + "'";
        MessageBox.Show(query);

And it worked somehow.