Exemple #1
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // identify the department id to be deleted from the row the user selected
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]);
            try
            {
             //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                Student stud = (from s in db.Students
                                where s.StudentID == StudentID
                                select s).FirstOrDefault();

                // delete
                db.Students.Remove(stud);
                db.SaveChanges();

                //refresh the grid
                GetStudents();
            }
            }
            catch
            {
                Response.Redirect("~/error.aspx");
            }
        }
        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");
            }
        }
Exemple #3
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                using (DefaultConnection db = new DefaultConnection())
                {

                    Student stud = new Student();

                    if (!String.IsNullOrEmpty(Request.QueryString["StudentID"]))
                    {

                        Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);
                        stud = (from s in db.Students
                                where s.StudentID == StudentID
                                select s).FirstOrDefault();
                    }

                    stud.LastName = txtName.Text;
                    stud.FirstMidName = txtFirstName.Text;
                    stud.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text);

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

                    // save new department
                    db.SaveChanges();

                    //redirect
                    Response.Redirect("students.aspx");
            }

            }
            catch
            {
                Response.Redirect("~/error.aspx");
            }
        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            try
            {
                using (DefaultConnection db = new DefaultConnection())
                {
                    Instructor objc = new Instructor();

                    objc.FirstName = txtFirstName.Text;
                    objc.LastName = txtLastName.Text;
                    objc.Username = txtUserName.Text;
                    objc.DepartmentID = Convert.ToInt32(ddlDepartments.SelectedValue);

                    //salt and hash password
                    String password = txtPassword.Text;
                    String salt = CreateSalt(8);
                    String pass_and_salt = password + salt;

                    // Create a new instance of the hash crypto service provider.
                    HashAlgorithm hashAlg = new SHA256CryptoServiceProvider();
                    // Convert the data to hash to an array of Bytes.
                    byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(pass_and_salt);
                    // Compute the Hash. This returns an array of Bytes.
                    byte[] bytHash = hashAlg.ComputeHash(bytValue);
                    // Optionally, represent the hash value as a base64-encoded string,
                    // For example, if you need to display the value or transmit it over a network.
                    string base64 = Convert.ToBase64String(bytHash);

                    objc.Password = base64;
                    objc.Salt = salt;

                    db.Instructors.Add(objc);
                    db.SaveChanges();
            }

            }
            catch
            {
                Response.Redirect("~/error.aspx");
            }
        }
        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 grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //identify the DepartmentID to be deleted from the row the user selected
            Int32 DepartmentID = Convert.ToInt32(grdDepartments.DataKeys[e.RowIndex].Values["DepartmentID"]);

            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                Department dep = (from d in db.Departments
                                  where d.DepartmentID == DepartmentID
                                  select d).FirstOrDefault();

                //delete
                db.Departments.Remove(dep);
                db.SaveChanges();

                //refresh grid
                GetDepartments();

            }
        }
        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");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            //use EF to connect to SQL Server
            using (DefaultConnection db = new DefaultConnection())
            {

                //use the Student model to save the new record
                Student s = new Student();
                Int32 StudentID = 0;

                //check the querystring for an id so we can determine add / update
                if (Request.QueryString["StudentID"] != null)
                {
                    //get the id from the url
                    StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    //get the current student from EF
                    s = (from objS in db.Students
                         where objS.StudentID == StudentID
                         select objS).FirstOrDefault();
                }

                s.LastName = txtLastName.Text;
                s.FirstMidName = txtFirstMidName.Text;
                s.EnrollmentDate = Convert.ToDateTime(txtEnrollmentDate.Text);

                //call add only if we have no student ID
                if (StudentID == 0)
                {
                    db.Students.Add(s);
                }

                //run the update or insert
                db.SaveChanges();

                //redirect to the updated students page
                Response.Redirect("students.aspx");
            }
        }
Exemple #9
0
        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");
            }
        }
        //        catch(Exception e)
        //{
        //    Response.Redirect("/error.aspx");
        //}
        //}
        //}
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //store which row was clicked
            Int32 selectedRow = e.RowIndex;

            //get the selected StudentID using the grid's Data Key collection
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[selectedRow].Values["StudentID"]);

            //use EF to remove the selected student from the db
            using (DefaultConnection db = new DefaultConnection())
            {

                Student s = (from objS in db.Students
                             where objS.StudentID == StudentID
                             select objS).FirstOrDefault();

                //do the delete
                db.Students.Remove(s);
                db.SaveChanges();
            }

            //refresh the grid
            GetStudents();
        }