protected void btnSave_Click(object sender, EventArgs e) { //use EF to connect to SQL server using (comp2007Entities db = new comp2007Entities()) { //use the student model to save the new record Department d = new Department(); Int32 DepartmentID = 0; //check the query string for an id so we can determine add / update if (Request.QueryString["DepartmentID"] != null) { //get the ID from the URL DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //get the current student from Entity Framework d = (from objS in db.Departments where objS.DepartmentID == DepartmentID select objS).FirstOrDefault(); } d.Name = txtDeptName.Text; d.Budget = Convert.ToDecimal(txtBudget.Text); //call add only if we have no student ID if (DepartmentID == 0) db.Departments.Add(d); db.SaveChanges(); //redirect to the updated students page Response.Redirect("departments.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { //connect using (DefaultConnection conn = new DefaultConnection()) { //instantiate a new deparment object in memory Department d = new Department(); //decide if updating or adding, then save if (Request.QueryString.Count > 0) { Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); d = (from dep in conn.Departments where dep.DepartmentID == DepartmentID select dep).FirstOrDefault(); } //fill the properties of our object from the form inputs d.Name = txtName.Text; d.Budget = Convert.ToDecimal(txtBudget.Text); if (Request.QueryString.Count == 0) { conn.Departments.Add(d); } conn.SaveChanges(); //redirect to updated departments page Response.Redirect("departments.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { // use EF to connect to SQL using (comp2007Entities db = new comp2007Entities()) { //use the student model to save the new model Department d = new Department(); Int32 DepartmentID = 0; //check save? or update?? if (Request.QueryString["DepartmentID"] != null) { //get the id from the url DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]); //get the current student from EF d = (from objS in db.Departments where objS.DepartmentID == DepartmentID select objS).FirstOrDefault(); } d.Name = txtDeptName.Text; d.Budget = Convert.ToDecimal(txtBudget.Text); if (DepartmentID == 0) { db.Departments.Add(d); } db.SaveChanges(); //redirect to the updated list page Response.Redirect("departments.aspx"); } }