コード例 #1
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
             // connect
            using (DefaultConnection db = new DefaultConnection())
            {
                // create a new department in memory
                Department dep = new Department();

                //check for url
                if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
                {
                    // get ud from url
                    Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);
                    dep = (from d in db.Departments
                           where d.DepartmentID == DepartmentID
                           select d).FirstOrDefault();
                }

                // fill the properties of the new department
                dep.Name = txtName.Text;
                dep.Budget = Convert.ToDecimal(txtBudget.Text);

                // add the new department if no url
                if (String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
                {
                    db.Departments.Add(dep);
                    db.SaveChanges();
                }

                // save new department
                db.SaveChanges();

                //redirect
                Response.Redirect("departments.aspx");
            }
            }
            catch
            {
                Response.Redirect("~/error.aspx");
            }
        }
コード例 #2
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                //create a new department in memory
                Department dep = new Department();

                Int32 DepartmentID = 0;

                //check for a url
                if (!String.IsNullOrEmpty(Request.QueryString["DepartmentID"]))
                {
                    //get the id from the url
                    DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

                    //look up the department
                    dep = (from d in db.Departments
                           where d.DepartmentID == DepartmentID
                           select d).FirstOrDefault();
                }

                //fill the properties of the new department
                dep.Name = txtName.Text;
                dep.Budget = Convert.ToDecimal(txtBudget.Text);

                //add if we have no id in the url
                if (DepartmentID == 0)
                {
                    db.Departments.Add(dep);
                }

                //save the new department
                db.SaveChanges();

                //redirect to the departments list page
                Response.Redirect("departments.aspx");
            }
        }