1

I have to update data in the data base. Data is shown in text boxes wen i select a data grid-view row. where am i wrong??

private void btnUpdate_Click(object sender, EventArgs e)
{
    ent = new EmployeeEntities();

        EmployeeInfo emp = new EmployeeInfo();
        emp.EmpID = Convert.ToInt32(txtID.Text);
        emp.EmpName = txtName.Text;
        emp.EmpAddress = txtAddress.Text;
        emp.EmpDesignation = txtDesignation.Text;
        //ent.EmployeeInfoes.Add(emp);
        ent.SaveChanges();
        MessageBox.Show("Updated");

}
2

3 Answers 3

2

The entity/ies you wish to save have to be attached to a data context in order to be saved upon calling SaveChanges. If calling ent.SaveChanges() calls the context's SaveChanges() then you only have to use ent.EmployeeInfoes.Add(emp);. If not, you need to retrieve the EmployeeEntities from the database first and then add the EmployInfo.

Sign up to request clarification or add additional context in comments.

Comments

1

replace

EmployeeInfo emp = new EmployeeInfo();

by

int empId=Convert.ToInt32(txtID.Text);
EmployeeInfo emp =ent.EmployeeInfoes.Single(e=>e.EmpId==empId);

Comments

0

if you are trying to add emp as a new emplouee, try

ent.EmployeeInfoes.AddObject(emp);
ent.SaveChanges();

else you must get emp from the list of employees then save it

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.