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