0

I want to update items by Entity Framework,

The code is correct but i don't have result in my database!!

using(checkinentrepriseEntities2 context = new checkinentrepriseEntities2()) {
 clients clien = new clients();

 clien.date_arrival = DateTime.Parse(textBoxDateIN.Text);
 clien.arrival_time = textBoxTIME.Text;
 clien.Aller_A = comboboxPersonnel.SelectedItem.ToString();
 clien.Badge = int.Parse(comboBoxBadge.SelectedItem.ToString());

 int badgeTiped = int.Parse(comboBoxBadge.SelectedItem.ToString());

 context.SaveChanges();
}
4
  • You're creating a new clients so you will be inserting and not updating in this situation, meaning you also need context.clients.Add(clien) or something along those lines Commented Nov 8, 2016 at 10:30
  • do you want add a new Client or update an existing Client? Commented Nov 8, 2016 at 10:30
  • context.clients.Add(clien) Commented Nov 8, 2016 at 10:30
  • OH! i make mistake... I want update an existing Client not Add !! Very Thakns!!!! Commented Nov 8, 2016 at 10:38

2 Answers 2

2

This code is creating a new clients object.

If you want to update results from the database you have to select the clients object from the database :

clients clien = context.clients.Single(x => x.Id == yourId);
// your logic
context.SaveChanges();
Sign up to request clarification or add additional context in comments.

1 Comment

That is it !! Thanks!
-1

You can try this code:

        using (checkinentrepriseEntities2 context = new checkinentrepriseEntities2())
        {
            clients clien = new clients();

            clien.date_arrival = DateTime.Parse(textBoxDateIN.Text);
            clien.arrival_time = textBoxTIME.Text;
            clien.Aller_A = comboboxPersonnel.SelectedItem.ToString();
            clien.Badge = int.Parse(comboBoxBadge.SelectedItem.ToString());

            int badgeTiped = int.Parse(comboBoxBadge.SelectedItem.ToString());

            context.clients.Add(clien);
            context.SaveChanges();
        }

Just add context.clients.Add(clien);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.