0

Following is the code to modify data using entity framework

int idtoupdate = Convert.ToInt32(Request.QueryString["EmpId"]);

EmployeeEntities db = new EmployeeEntities();

Employee emp = db.Employees.SingleOrDefault(p => p.EmpId == idtoupdate);

I am getting error for below line.......

emp.EmpLoc = TextBox1.Text;
Label2.Text = Convert.ToString(emp.EmpId);
Label4.Text = emp.EmpName;
db.SaveChanges();
halfer
  • 19,824
  • 17
  • 99
  • 186
P33k
  • 1
  • 1
  • yes m using try catch block for null reference exception but then the values for label2 and label4 are not displaying – P33k Feb 19 '16 at 09:19

1 Answers1

0

As I can see you don't have Employee with EmpId == idtoupdate in your database. Before using emp.EmpLoc you have to check is emp is null:

if (emp != null) {
  emp.EmpLoc = TextBox1.Text;
  Label2.Text = Convert.ToString(emp.EmpId);
  Label4.Text = emp.EmpName;
  db.SaveChanges();
}
feeeper
  • 2,865
  • 4
  • 28
  • 42