Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

         private void showAll(ComboBox cmb, string col) {
             cmb.Items.Clear();

             OleDbCommand cmd;
             OleDbDataReader reader;

             try {
                 using (OleDbConnection cn = new OleDbConnection(conString)) {
                     cmd = new OleDbCommand("SELECT DISTINCT " + col + " FROM tblstudent ORDER BY " + col + ";", cn);
                     cn.Open();
                     reader = cmd.ExecuteReader();

                     if (reader.HasRows) {
                         while (reader.Read()) {
                             string item = reader[col].ToString();

                             cmb.Items.Add(item);
                         }
                     }
                     cn.Close();
                 }
             } catch (Exception ex) {
                 MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }