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); } }
new protected void Page_Init(object sender, EventArgs e) { base.Page_Init(sender, e); using (WebhostEntities db = new WebhostEntities()) { StudentComboBox.DataSource = StudentListItem.GetDataSource( db.Students.Where(s => s.isActive).OrderBy(s => s.GraduationYear).ThenBy(s => s.LastName).ThenBy(s => s.FirstName).Select(s => s.ID).ToList()); StudentComboBox.DataTextField = "Text"; StudentComboBox.DataValueField = "ID"; StudentComboBox.DataBind(); } }
new protected void Page_Init(object sender, EventArgs e) { base.Page_Init(sender, e); using (WebhostEntities db = new WebhostEntities()) { Faculty faculty = db.Faculties.Find(user.ID); if (faculty == null) { LogError("Could not locate faculty id {0}", user.ID); Response.Redirect("~/Home.aspx"); } StudentComboBox.DataSource = StudentListItem.GetDataSource(faculty.Students.Where(s => s.isActive).Select(s => s.ID).ToList()); StudentComboBox.DataTextField = "Text"; StudentComboBox.DataValueField = "ID"; StudentComboBox.DataBind(); } }
// 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); } }