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 Autocomplete()
         {
             try{
             con = new SqlConnection(cs.DBConn);
             con.Open();
             SqlCommand cmd = new SqlCommand("SELECT username FROM registration", con);
             DataSet ds = new DataSet();
             SqlDataAdapter da = new SqlDataAdapter(cmd);
             da.Fill(ds, "registration");
             AutoCompleteStringCollection col = new AutoCompleteStringCollection();
             int i = 0;
             for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
             {
                 col.Add(ds.Tables[0].Rows[i]["Username"].ToString());

             }
             txtUsername.AutoCompleteSource = AutoCompleteSource.CustomSource;
             txtUsername.AutoCompleteCustomSource = col;
             txtUsername.AutoCompleteMode = AutoCompleteMode.Suggest;

             con.Close();
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }