private void Course_Load(object sender, EventArgs e) { dataGridView1.Hide(); SubjectComboBox.Hide(); StudentComboBox.Hide(); TeacherNameLabel.Hide(); Hide_Eval_And_Insert_Marks_And_Update_Marks_Code(); try { //sqlConnection1.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlConnection1; cmd.CommandType = CommandType.Text; cmd.CommandText = "Select course_name from Course"; SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { CourseComboBox.Items.Add(reader.GetString(0)); } } reader.Close(); //sqlConnection1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
// Replaced Method private void SubjectComboBox_SelectedValueChanged(object sender, EventArgs e) { StudentComboBox.Show(); StudentComboBox.Items.Clear(); Hide_Eval_And_Insert_Marks_And_Update_Marks_Code(); dataGridView1.Rows.Clear(); try { //sqlConnection1.Open(); Data_Grid_View_Initialization(); SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlConnection1; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT teacher_name from Teacher where teacher_id = (Select teacher_id from Subject where subject_name = @sub_name)"; cmd.Parameters.AddWithValue("@sub_name", SubjectComboBox.SelectedItem); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { TeacherNameLabel.Text = "Welcome Mrs. " + reader.GetString(0); } TeacherNameLabel.Show(); reader.Close(); cmd.CommandText = "Select student_name from Student where course_id = (SELECT course_id from Subject where subject_name = @s_name)"; cmd.Parameters.AddWithValue("@s_name", SubjectComboBox.SelectedItem); reader = cmd.ExecuteReader(); while (reader.Read()) { StudentComboBox.Items.Add(reader.GetString(0)); } reader.Close(); //sqlConnection1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
//Replaced Method private void CourseComboBox_SelectedValueChanged(object sender, EventArgs e) { SubjectComboBox.Show(); TeacherNameLabel.Hide(); dataGridView1.Hide(); StudentComboBox.Items.Clear(); SubjectComboBox.Items.Clear(); Hide_Eval_And_Insert_Marks_And_Update_Marks_Code(); try { SubjectLabel.Text = "Select Subject for " + CourseComboBox.SelectedItem; int selected_course_id = CourseComboBox.SelectedIndex + 1; SqlCommand cmd = new SqlCommand(); cmd.Connection = sqlConnection1; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT subject_name from Subject where course_id =@c_id"; cmd.Parameters.AddWithValue("@c_id", selected_course_id); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { SubjectComboBox.Items.Add(reader.GetString(0)); } } reader.Close(); //sqlConnection1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }