My result in I get in my console.
UPDATE [Client] SET username ='asd', password ='asd', address ='asddd', referenceno ='12345' WHERE id = 27
When I write a query in my MS Access Database, it is working fine.
I have no idea why this error appears whenever I tried to update my data into the database.
private void buttonUpdate_Click(object sender, EventArgs e) // user click on button update
{
if (cbTable.Text.Equals("User"))
{
string query = "";
query += "username ='" + textBoxUsername.Text.ToString() + "' ,"; //query
query += "password ='" + textBoxPassword.Text.ToString() + "' ,"; //query
query += "contact ='" + ContactNo.Text.ToString() + "' ,"; //query
query += "ref_no = " + textBoxReferenceno.Text.ToString() + " WHERE id = " + Convert.ToInt32(textBoxId.Text.ToString()); //query
try
{
new controllerclass().updateDatabase("User", query); //update database
Console.WriteLine(query);
Console.WriteLine("Saved");
MessageBox.Show("User profile has been updated.", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
loadDatabaseUser();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
//After users enter the update button, this function will be used.
public bool updateDatabase(string type, string query) //update database function
{
try
{
OleDbCommand cmd = new OleDbCommand(); //open connection
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE [" + type + "] SET " + query;
cmd.Connection = conn;
Console.WriteLine("UPDATE [" + type + "] SET " + query);
cmd.ExecuteNonQuery(); //execute command
closeConnection();
return true;
}
catch (Exception e)
{
closeConnection(); // close connection
Console.WriteLine(e.Message); //writeline to console
return false;
}
}