protected void btnSave_Click(object sender, EventArgs e) { //connect using (DefaultConnection db = new DefaultConnection()) { //create a new course and fill the properties Course objC = new Course(); objC.Title = txtTitle.Text; objC.Credits = Convert.ToInt32(txtCredits.Text); objC.DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue); //save db.Courses.Add(objC); db.SaveChanges(); //redirect Response.Redirect("courses.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { try { using (DefaultConnection db = new DefaultConnection()) { Course objc = new Course(); objc.Title = txtTitle.Text; objc.Credits = Convert.ToInt32(txtCredits.Text); objc.DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue); db.Courses.Add(objc); db.SaveChanges(); Response.Redirect("courses.aspx"); } } catch { Response.Redirect("~/error.aspx"); } }