protected void btnSave_Click(object sender, EventArgs e) { //connect using (DefaultConnection db = new DefaultConnection()) { //create new department in memory Department dep = new Department(); //check url if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"])) { Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); dep = (from d in db.Departments where d.DepartmentID == DepartmentID select d).FirstOrDefault(); } //fill new properties of the new department dep.Name = txtName.Text; dep.Budget = Convert.ToDecimal(txtBudget.Text); //save the new department if (String.IsNullOrEmpty(Request.QueryString["DepartmentID"])) { db.Departments.Add(dep); } db.Departments.Add(dep); db.SaveChanges(); //redirect to department list page Response.Redirect("departments.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { //connect using (DefaultConnection db = new DefaultConnection()) { //create a new department in memory Department dep = new Department(); // op2 :Int32 DepartmentID = 0; //check fot a url if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"])) { Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); // op2: DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); dep = (from d in db.Departments where d.DepartmentID == DepartmentID select d).FirstOrDefault(); } //fill the property of the new department dep.Name = txtname.Text; dep.Budget = Convert.ToDecimal(txtBudget.Text); //add if we have no id in the url if (String.IsNullOrEmpty(Request.QueryString["DepartmentID"])) { db.Departments.Add(dep); } //op2: if(DepartmentID==0) // save the new department //excute the change commands db.SaveChanges(); //redirect to the departments list page Response.Redirect("departments.aspx"); } }