Domanda

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

È stato utile?

Soluzione

Your insert command is not a valid sql INSERT statement.

command.CommandText = "INSERT INTO tblContact VALUES(@contactid, @lastname, @firstname,"+ 
                      "@address,@postcode, @city, @gender, @blocked)";

The update commad text is missing a comma

command.CommandText = "UPDATE tblContact SET LastName = @lastname, FirstName = @firstname," +  
                      "Address = @address, PostCode = @postcode, City = @city, " + 
                      "Gender = @gender, Blocked = @blocked WHERE Id = @contactid";

However, the command fails also because some OleDb provider (like Microsoft.ACE.OleDb.xx) require the ParameterCollection to have the parameters in the exact order in which they appear in the sql text. (No support for named parameters). Your update statement contains the @contactid as last parameter while you add it as first. You could try to change the AddWithValue sequence adding @contactid as last parameter.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top