So I'm trying to add a contact to my Database using a DataAdapter and Datasets. But I keep getting the same error when I try to add data (see title)
Also when I update my data it doesn't give any error, but it also doesn't update anything.
The Add user code
public void AddUser(Contact contact) {
command.Connection = getConnection();
command.CommandText = "INSERT INTO tblContact VALUES(Id = @contactid, LastName = @lastname, FirstName = @firstname, Address = @address"
+ "PostCode = @postcode, City = @city, Gender = @gender, Blocked = @blocked)";
command.Parameters.AddWithValue("@contactid", contact.AccountId);
command.Parameters.AddWithValue("@lastname", contact.LastName);
command.Parameters.AddWithValue("@firstname", contact.FirstName);
command.Parameters.AddWithValue("@address", contact.Address);
command.Parameters.AddWithValue("@postcode", contact.PostCode);
command.Parameters.AddWithValue("@city", contact.City);
bool gender = (contact.Gender == Gender.Male ? true : false);
command.Parameters.AddWithValue("@gender", gender);
command.Parameters.AddWithValue("@blocked", contact.Blocked);
try {
adapter.InsertCommand = command;
adapter.Update(dsCon, "tblContact");
adapter.Fill(dsCon, "tblContact");
}
catch (Exception e) {
String code = e.Message;
}
}
The code used to update a user
public void ModifyUser(Contact contact)
{
command.Connection = getConnection();
command.CommandText = "UPDATE tblContact SET LastName = @lastname, FirstName = @firstname, Address = @address"
+ "PostCode = @postcode, City = @city, Gender = @gender, Blocked = @blocked "
+ "WHERE Id = @contactid";
command.Parameters.AddWithValue("@contactid", contact.AccountId);
command.Parameters.AddWithValue("@lastname", contact.LastName);
command.Parameters.AddWithValue("@firstname", contact.FirstName);
command.Parameters.AddWithValue("@address", contact.Address);
command.Parameters.AddWithValue("@postcode", contact.PostCode);
command.Parameters.AddWithValue("@city", contact.City);
command.Parameters.AddWithValue("@gender", contact.Gender);
command.Parameters.AddWithValue("@blocked", contact.Blocked);
adapter.UpdateCommand = command;
adapter.Update(dsCon, "tblContact");
adapter.Fill(dsCon, "tblContact");
}
The code in my form that initiates these processes
private void btnSave_Click(object sender, EventArgs e) {
Contact currentContact = new Contact();
currentContact.AccountId = Int32.Parse(lblID.Text);
currentContact.LastName = txtLastName.Text;
currentContact.FirstName = txtName.Text;
currentContact.Address = txtStreet.Text;
currentContact.PostCode = Int32.Parse(txtPostalCode.Text);
currentContact.City = txtCity.Text;
currentContact.Gender = (rdbMale.Checked == true ? Gender.Male : Gender.Female);
currentContact.Blocked = chkBlocked.Checked;
currentContact.Address = txtStreet.Text;
if(isNewContact){
currentContact.AccountId = (manager.GetContacts().Last().AccountId + 1);
manager.GetOleDbManager().AddUser(currentContact);
} else {
manager.GetOleDbManager().ModifyUser(currentContact);
}
currentContact.Categories = new List<Category>();
foreach (Object c in lstCategories.SelectedItems) {
currentContact.Categories.Add((Category)c);
}
isNewContact = false;
}
If you could help it would be fantastic, here is a screenshot of the db I'm using
http://i50.tinypic.com/2s93klk.png