private void loadWorkerCombo() { string workerSelect = "SELECT S_NAME, FIRST_NAME, LAST_NAME " + "FROM WORKER"; command = new SqlCommand(workerSelect, connection); //open the connection to the database try { connection.Open(); //create the SqlDataReader object using the command object SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); //use loop to read data while (reader.Read()) { //create temp object and call constructor StudentWorker tempWorker = new StudentWorker(); //read the rows and save in tempMemb tempWorker.WUserName = reader["S_NAME"].ToString(); tempWorker.WFirstName = reader["FIRST_NAME"].ToString(); tempWorker.WLastName = reader["LAST_NAME"].ToString(); //add tempWorker object to the list workers.Add(tempWorker); } //close the database connection.Close(); //load the name in the combo box foreach (StudentWorker aWorker in workers) { string workerName = aWorker.WFirstName + " " + aWorker.WLastName; cboStudWorker.Items.Add(workerName); } } catch (Exception ex) { MessageBox.Show("Error Loading Workers: " + ex); } }
//Gets all workers from WORKERS table private void collectWorkers() { //Query to get all student worker usernames string workerSelect = "SELECT S_NAME, FIRST_NAME, LAST_NAME, PASSWORD FROM WORKER"; command = new SqlCommand(workerSelect, connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); //Reads the usernames and stores them while (reader.Read()) { StudentWorker tempWork = new StudentWorker(); tempWork.WUserName = reader["S_NAME"].ToString(); tempWork.WFirstName = reader["FIRST_NAME"].ToString(); tempWork.WLastName = reader["LAST_NAME"].ToString(); tempWork.Password = reader["PASSWORD"].ToString(); workers.Add(tempWork); } loadWorkers(); } catch (Exception ex) { MessageBox.Show("Error 1 Worker Display: \n" + ex); } finally { connection.Close(); } }