Trouble in updating database record in access with c#

I am working with a program which stores customers data and later it needs to be changed

this is my code :

private void button2_Click(object sender, EventArgs e)       
{
        conn.Open();
        string oc = ("Update Custs Set Cname =@Cname, NatCode=@NatCode, Bdate=@Bdate, CellPhone=@CellPhone, Addr=@Addr,  Where NatCode Like'%" + textBox2.Text + "%'");

        OleDbCommand myCommand = new OleDbCommand();
        myCommand.CommandText = oc;
        myCommand.Connection = conn;
        myCommand.Parameters.AddWithValue("@Cname",textBox1.Text);
        myCommand.Parameters.AddWithValue("@NatCode", textBox3.Text);
        myCommand.Parameters.AddWithValue("@Bdate", textBox2.Text);
        myCommand.Parameters.AddWithValue("@CellPhone", textBox4.Text);
        myCommand.Parameters.AddWithValue("@Addr", textBox8.Text);

        myCommand.ExecuteNonQuery();

        conn.Close();
        MessageBox.Show("Data Changed successfully!");

        }
    }
}

But it doesn't do change anything

any help would be appreciated

1 answer

  • answered 2017-08-12 09:48 PurTahan

    Are you sure Where NatCode Like'%" + textBox2.Text + "%'" contains any row?

    in myCommand.Parameters.AddWithValue("@Bdate", textBox2.Text); you are using textBox2 for Birthday and in Query you use it for NationalCode.

    I guess the problem is it.