I'm trying to update a table in MS Access, but the update operation doesn't work and I don't get an error.
The rowsAffected are 0, so nothing was updated.
Here is my code:
public bool UpdateAddress(AddressModel address)
{
using (OleDbCommand dbCommand = new OleDbCommand())
{
// Set the command object properties
dbCommand.Connection = new OleDbConnection(this.ConnectionString);
dbCommand.CommandType = CommandType.Text;
dbCommand.CommandText = "Update Addresses " +
" Set [Street] = @Street, [PostalCode] = @PostalCode, " +
" [City] = @City, [Contact] = @Contact"+
" Where [Address_ID] = @Address_ID";
// Add the input parameters to the parameter collection
dbCommand.Parameters.AddWithValue("@Street", address.Street);
dbCommand.Parameters.AddWithValue("@PostalCode", address.PostalCode);
dbCommand.Parameters.AddWithValue("@City", address.City);
dbCommand.Parameters.AddWithValue("@Address_ID", address.Address_ID);
dbCommand.Parameters.AddWithValue("@Contact", address.Contact);
// Open the connection, execute the query and close the connection
dbCommand.Connection.Open();
var rowsAffected = dbCommand.ExecuteNonQuery();
dbCommand.Connection.Close();
return rowsAffected > 0;
}
}
AddressModel-Class:
public class AddressModel
{
public Int64 Address_ID { get; set; }
public Int64 Customer_ID { get; set; }
public string Street { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
public string Contact { get; set; }
public bool FirstAddress { get; set; }
}
What could be the problem?
address.Address_IDis at runtime