Example #1
0
        protected void Course_Info()
        {
            string StuId = Session["userId"].ToString();
            string sql   = "select * from CourseInfo inner join SCInfo on CourseInfo.courseId = SCInfo.CourseId inner join StudentInfo on StudentInfo.StuId = SCInfo.StuId where SCInfo.StuId='" + StuId + "'";

            CourseInfo.DataSource   = DataOperate.GetDataset(sql, "CourseInfo");
            CourseInfo.DataKeyField = "courseId";
            CourseInfo.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            int           courseID = Convert.ToInt32(Request.QueryString["CourseID"]);
            SqlConnection conn;
            SqlCommand    comm_students;
            SqlCommand    comm_course;
            SqlDataReader reader1;
            SqlDataReader reader2;
            // read the connection string from Web.config
            string connectionString = ConfigurationManager.ConnectionStrings["Students"].ConnectionString;

            // Initialize connection
            conn = new SqlConnection(connectionString);
            //create command
            comm_students = new SqlCommand("SELECT Students.StudentID,Students.FirstMidName,Students.LastName FROM Enrollments JOIN Students on Enrollments.StudentID = Students.StudentID WHERE Enrollments.CourseID = @CourseID", conn);
            // add parameter into command
            comm_course = new SqlCommand("SELECT CourseID,Title FROM Courses WHERE CourseID = @CourseID", conn);
            comm_students.Parameters.Add("@CourseID", System.Data.SqlDbType.Int);
            comm_students.Parameters["@CourseID"].Value = courseID;
            comm_course.Parameters.Add("@CourseID", System.Data.SqlDbType.Int);
            comm_course.Parameters["@CourseID"].Value = courseID;
            try
            {
                //open connection
                conn.Open();
                //execute the command
                reader1 = comm_students.ExecuteReader();
                // bind the reader to DataList
                StudentList.DataSource = reader1;
                StudentList.DataBind();
                reader1.Close();

                reader2 = comm_course.ExecuteReader();
                CourseInfo.DataSource = reader2;
                CourseInfo.DataBind();
                //Close the reader
                reader2.Close();
            }
            finally
            {
                //close connection
                conn.Close();
            }
        }