protected void GetStudent()
        {
            try
            {
                //look up selected student and fill form
                using (DefaultConnection db = new DefaultConnection())
                {
                    //store id from the url in a variable
                    Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                    //look up student
                    Student stud = (from s in db.Students
                                    where s.StudentID == StudentID
                                    select s).FirstOrDefault();
                    //populate form fields
                    txtLast.Text = stud.LastName;
                    txtFirst.Text = stud.FirstMidName;
                    txtEnroll.Text = stud.EnrollmentDate.ToString();

                    //populate all courses student is enrolled in
                    var objK = (from en in db.Enrollments
                                join c in db.Courses on en.CourseID equals c.CourseID
                                join d in db.Departments on c.DepartmentID equals d.DepartmentID
                                where en.StudentID == StudentID
                                select new { en.EnrollmentID, en.Grade, c.Title, d.Name });

                    grdCourses.DataSource = objK.ToList();
                    grdCourses.DataBind();
                }
            }
            catch (Exception e)
            {
                Response.Redirect("error.aspx");
            }
        }
        protected void GetDepartment()
        {
            try
            {
                //look up the selected department and fill the form
                using (DefaultConnection db = new DefaultConnection())
                {
                    //store the id from the url in a variable
                    Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

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

                    //pre-populate the form fields
                    txtName.Text = dep.Name;
                    txtBudget.Text = dep.Budget.ToString();
                }
            }
            catch (Exception e)
            {
                Response.Redirect("error.aspx");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //connect
                using (DefaultConnection db = new DefaultConnection())
                {
                    //create new course and fill 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");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("error.aspx");
            }
        }
Exemple #4
0
        protected void GetStudents()
        {
            try
            {
                //use entity framework to connect and get list of Departments
                using (DefaultConnection db = new DefaultConnection())
                {
                    var stud = from d in db.Students
                               select d;

                    //bind the stud query result to grid
                    grdStudents.DataSource = stud.ToList();
                    grdStudents.DataBind();
                }
            }
            catch (NullReferenceException e)
            {
                Trace.Write("An error occured during operation with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
            catch (Exception e)
            {
                Trace.Write("Database unavailable with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            using (DefaultConnection db = new DefaultConnection())
            {
                Instructor objI = new Instructor();

                objI.FirstName = txtFirstName.Text;
                objI.LastName = txtLastName.Text;
                objI.Username = txtUsername.Text;
                objI.DepartmentID = Convert.ToInt32(ddlDepartment.SelectedValue);

                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);

                objI.Password = base64;
                objI.Salt = salt;

                db.Instructors.Add(objI);
                db.SaveChanges();
            }
        }
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //identify id of student to delete from the row the user has chosen
                Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]);

                //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 grid
                    Response.Redirect("students.aspx");

                }
            }
            catch (Exception ex)
            {
                Response.Redirect("error.aspx");
            }
        }
        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 GetDepartment()
        {
            try
            {
                //look up the selected department and fill the form
                using (DefaultConnection db = new DefaultConnection())
                {
                    Int32 DepartmentID = Convert.ToInt32(Request.QueryString["DepartmentID"]);

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

                    //pre-populate the form fields
                    txtName.Text = dep.Name;
                    txtBudget.Text = dep.Budget.ToString();
                }
            }
            catch (NullReferenceException e)
            {
                Trace.Write("An error occured during operation with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
            catch (Exception e)
            {
                Trace.Write("Database unavailable with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
        }
Exemple #9
0
        protected void GetCourses()
        {
            try
            {
                using (DefaultConnection db = new DefaultConnection())
                {
                    var cours = from d in db.Courses
                                select d;

                    //bind the cours query result to our grid
                    grdCourses.DataSource = cours.ToList();
                    grdCourses.DataBind();
                }
            }
            catch (NullReferenceException e)
            {
                Trace.Write("An error occured during operation with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
            catch (Exception e)
            {
                Trace.Write("Database unavailable with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
        }
Exemple #10
0
        protected void GetDepartments()
        {
            try
            {
                //use entity framework to connect and get the list of departments
                using (DefaultConnection db = new DefaultConnection())
                {
                    //old query that show all department
                    //var deps = from d in db.Departments
                    //          select d;
                    //new query filtered for logged in user only
                    Int32 DepartmentID = Convert.ToInt32(Session["DepartmentID"]);

                    var deps = from d in db.Departments
                               where d.DepartmentID == DepartmentID
                               select d;

                    //bind the deps query result to our grid
                    grdDepartments.DataSource = deps.ToList();
                    grdDepartments.DataBind();
                }
            }
            catch (NullReferenceException e)
            {
                Trace.Write("An error occured during operation with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);

            }
            catch (Exception e)
            {
                Trace.Write("Database unavailable with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
        }
Exemple #11
0
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            using (DefaultConnection db = new DefaultConnection())
            {
                //create instructor name
                Instructor objI = new Instructor();

                //first get salt value
                String username = txtUsername.Text;

                objI = (from i in db.Instructors
                        where i.Username == username
                        select i).FirstOrDefault();

                //did we find the username
                if (objI != null)
                {
                    String salt = objI.Salt;

                    String password = txtPassword.Text;
                    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);

                    //check if the password we just salted and hashed matches the password in db
                    if (objI.Password == base64)
                    {
                        //test with label
                        //lblError.Text = "valid login";
                        //store the identity in the session object
                        Session["InstructorID"] = objI.InstructorID;
                        Session["InstructorName"] = objI.FirstName + objI.LastName;
                        Session["DepartmentID"] = objI.DepartmentID;

                        //redirect to department page
                        Response.Redirect("departments.aspx");
                    }
                    else
                    {
                        lblError.Text = "invalid login";
                    }
                }
                else
                {
                    lblError.Text = "Invalid login";
                }
            }
        }
Exemple #12
0
        protected void GetStudent()
        {
            try
            {
                //store id from the url in a variable
                Int32 StudentID = Convert.ToInt32(Request.QueryString["StudentID"]);

                //look up selected student and fill the form
                using (DefaultConnection db = new DefaultConnection())
                {

                    //populate student instance
                    Student stud = (from d in db.Students
                                    where d.StudentID == StudentID
                                    select d).FirstOrDefault();

                    if (stud != null)
                    {
                        //pre-populate the form fields
                        txtFirstName.Text = stud.FirstMidName;
                        txtLastName.Text = stud.LastName;
                        txtEnrollDate.Text = stud.EnrollmentDate.ToString("yyyy-mm-dd");
                    }

                    //populate enrollments
                    var enroll = (from e in db.Enrollments
                                  join c in db.Courses on e.CourseID equals c.CourseID
                                  join d in db.Departments on c.DepartmentID equals d.DepartmentID
                                  where e.StudentID == StudentID
                                  select new { e.EnrollmentID, e.Grade, c.Title, d.Name });

                    grdCourses.DataSource = enroll.ToList();
                    grdCourses.DataBind();

                    //show course panel
                    pnlCourses.Visible = true;
                }
            }
            catch (NullReferenceException e)
            {
                Trace.Write("An error occured during operation with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
            catch (Exception e)
            {
                Trace.Write("Database unavailable with Message:", e.Message);
                Trace.Write("Stack Trace", e.StackTrace);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                using (DefaultConnection db = new DefaultConnection())
                {
                    //create a new student in memory
                    Student stud = new Student();

                    Int32 StudentID = 0;

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

                        //look up the student
                        stud = (from s in db.Students
                                where s.StudentID == StudentID
                                select s).FirstOrDefault();
                    }

                    //fill the properties of the new student
                    stud.LastName = txtLast.Text;
                    stud.FirstMidName = txtFirst.Text;
                    stud.EnrollmentDate = Convert.ToDateTime(txtEnroll.Text);

                    //add if we have no id in the url
                    if (StudentID == 0)
                    {

                        db.Students.Add(stud);
                    }

                    //save the student
                    db.SaveChanges();

                    //redirect to student list page
                    Response.Redirect("students.aspx");

                }
            }
            catch (Exception ex)
            {
                Response.Redirect("error.aspx");
            }
        }
        protected void GetDepartment()
        {
            using (DefaultConnection db = new DefaultConnection())
            {
                var deps = from d in db.Departments
                           orderby d.Name
                           select d;

                ddlDepartment.DataSource = deps.ToList();
                ddlDepartment.DataBind();

                ListItem default_item = new ListItem("select", "0");
                ddlDepartment.Items.Insert(0, default_item);

            }
        }
        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();

                    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");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("error.aspx");
            }
        }
 protected void GetStudents()
 {
     try
     {
         //use enity framework to connect to get list of students
         using (DefaultConnection db = new DefaultConnection())
         {
             var stud = from s in db.Students
                        select s;
             //bind stud query results to our grid
             grdStudents.DataSource = stud.ToList();
             grdStudents.DataBind();
         }
     }
     catch (Exception e)
     {
         Response.Redirect("error.aspx");
     }
 }
Exemple #17
0
        protected void grdDepartments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //identify the departmentID to be deleted from the row 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();
            }
        }
Exemple #18
0
        protected void grdStudents_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //identify the studentID to be deleted from the row the user selected
            Int32 StudentID = Convert.ToInt32(grdStudents.DataKeys[e.RowIndex].Values["StudentID"]);

            //connect to database
            using (DefaultConnection db = new DefaultConnection())
            {
                Student stud = (from d in db.Students
                                where d.StudentID == StudentID
                                select d).FirstOrDefault();

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

                //refresh grid
                GetStudents();
            }
        }
        protected void GetDepartments()
        {
            //use Entity Framework to connect and get the list of Departments
            using (DefaultConnection db = new DefaultConnection())
            {
                //old query that shows all departments
                //var deps = from d in db.Departments
                // select d;

                //new query filterd for logged in user only
                Int32 DepartmentID = Convert.ToInt32(Session["DepartmentID"]);
                var deps = from d in db.Departments
                           where d.DepartmentID == DepartmentID
                           select d;

                //bind the deps query result to our grid
                grdDepartments.DataSource = deps.ToList();
                grdDepartments.DataBind();
            }
        }
        protected void GetCourse()
        {
            try
            {

            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                //get the selected courseid from url
                Int32 CourseID = Convert.ToInt32(Request.QueryString["CourseID"]);

                //query the db
                Course objC = (from c in db.Courses
                               where c.CourseID == CourseID
                               select c).FirstOrDefault();

                // populate the form
                txtTitle.Text = objC.Title;
                txtCredits.Text = objC.Credits.ToString();
                ddlDepartment.SelectedValue = objC.DepartmentID.ToString();

                //populate student enrollments grid
                var Enrollments = from en in db.Enrollments
                                  where en.CourseID == CourseID
                                  orderby en.Student.LastName,
                                  en.Student.FirstMidName
                                  select en;

                //bind to the grid
                grdEnrollments.DataSource = Enrollments.ToList();
                grdEnrollments.DataBind();
            }
            } catch (Exception e) {
            //redirect
            Response.Redirect("error.aspx");
            }
        }
        protected void GetDepartments()
        {
            try {
            //connect
            using (DefaultConnection db = new DefaultConnection())
            {
                //get department list
                var Departments = from d in db.Departments
                                  orderby d.Name
                                  select d;

                //bind dropdown list
                ddlDepartment.DataSource = Departments.ToList();
                ddlDepartment.DataBind();

                //add default option for dropdown after it's filled
                ListItem DefaultItem = new ListItem("-Select-", "0");
                ddlDepartment.Items.Insert(0, DefaultItem);
            }
            }
            catch (Exception e)
            {
                Response.Redirect("error.aspx");
            }
        }
        protected void grdCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                //get selected id

                Int32 EnrollmentID = Convert.ToInt32(grdCourses.DataKeys[e.RowIndex].Values["EnrollmentID"]);

                //connect
                using (DefaultConnection db = new DefaultConnection())
                {
                    //get record
                    Enrollment objK = (from en in db.Enrollments
                                       where en.EnrollmentID == EnrollmentID
                                       select en).FirstOrDefault();
                    //delete
                    db.Enrollments.Remove(objK);
                    db.SaveChanges();

                    //refresh
                    GetStudent();
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("error.aspx");
            }
        }