Click here to Skip to main content
15,915,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to update record number 4 from a list of 6 but my save changes method below is overwriting my 1st record with the updates instead. I know where I am going wrong(see the bold) but just not sure how to correct it, can someone please advise me?
C#
private void btnSave_Click(object sender, EventArgs e)
{
    var contact = context.Contacts.First();
    var address = context.Address.First();    
    string[] name = txtTenant.Text.Split(' ');
    contact.FirstName = name[0].ToString();
    contact.LastName = name[1].ToString();
    address.Address1 = txtAddress.Text;                                              
    contact.ContactNumber = txtContactNum.Text;
    address.Mortgage = txtMortgage.Text;
    address.RemainingMortgage = txtRemainingMortgage.Text;
    address.Rent = txtRent.Text;
    address.Period = cboPeriod.SelectedIndex;
    address.RentArrears = txtRentArrears.Text;
    address.Rates = txtRates.Text;
    address.RatesUpToDate = chkRates.Checked;
    address.RentArrears = txtRentArrears.Text;
    address.Repairs = txtRepairs.Text;
    
    context.SaveChanges();
    SetContactsDDL(cboContactList.SelectedIndex);
}
Posted
Comments
Ziee-M 11-Feb-14 10:52am    
hi, you are selecting your first element in the list : var contact = context.Contacts.First();
try using link to select the 4th record.
var contact = (from cont in context.Contcts
WHere cont.Id = 4 //I don't know your Identifier, you have to use yours
select cont).SingleOrDefault();
pmcm 11-Feb-14 11:38am    
thanks for this, an update to your help:
var contact = (from cont in context.Contacts
where cont.ContactID = cboContactList.SelectedIndex
select cont).SingleOrDefault();

but this is giving me the error cannot implicity convert type 'int' to 'bool'. I've tried wrapping convert.toint32 around my oContactList.SelectedIndex.
idenizeni 11-Feb-14 12:02pm    
var contact = (from cont in context.Contacts
where cont.ContactID == Convert.ToInt32(cboContactList.SelectedValue)
select cont).SingleOrDefault();

Use double equal operators to check for equality. Also, you may want to use the SelectedValue property instead of the SelectedIndex as the fourth item in your combobox may not have an id equal to 4.
Ziee-M 11-Feb-14 11:58am    
sorry for the error, you should write where cont.ContactID == cboContactList.SelectedIndex
== instead of =
Ziee-M 11-Feb-14 11:59am    
you have to write == instead of = in the where clause!

1 solution

This is a Entity Framework 101 topic. I seriously suggest picking up the books by Julia Lerman(?) on Entity Framework and workign through them.

I have no idea how you got the record from the database, but it should have it's Id (primary key field) number in some field. Use that to lookup the record using the context.
Contact contactToUpdate = context.Contacts.Find(contactId);
...
contact.FirstName = name[0].ToString();
... blah blah blah ...


// Depending on how you're doing things, you may need to do this to
// force EF to consider the record "dirty".
context.Entry(contactToUpdate).State = EntityState.Modified;

context.SaveChanges();
 
Share this answer
 
Comments
pmcm 12-Feb-14 5:07am    
Thanks for the book recommendation!
pmcm 13-Feb-14 6:47am    
Hi, I'm trying to implement your suggestion above but I am getting this error message:
"'System.Data.Objects.ObjectQuery<housemanager.contact>' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Data.Objects.ObjectQuery<housemanager.contact>' could be found (are you missing a using directive or an assembly reference?)"

I've added: using System.Data.Entity;
What else am I missing?
Dave Kreskowiak 13-Feb-14 8:43am    
Chances are good you're using Entity Framework 4.0 or below. If so, there is no Find() method. I didn't start using EF until 4.3 came out, which already ha a Find method. Now that 6.x is out, you're a couple years behind the times.

I don't know what the method is to find an object in EF4 using it's primary key.
pmcm 13-Feb-14 9:05am    
Thanks for responding, I was able to use the other method provided to help me instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900