title: foreach (variable in checklistbox.checkedItems) in c# vs2012
body: <p>I'm trying to make a program where the user can select several items in a combo listbox, retrieves its id from my SQL database, returns the id, and inserts the id in another table. I'm not sure if this is the most effective way, and suggestions are welcome. However, I ran into the problem in how to complete my foreach statement. What should I place in the position marked by X below? (in the <code>foreach</code> statement)</p>
  
  <p>Here's my code: (<code>clbGeneral</code> is the name of the checklistbox)</p>
  
  <pre><code>if (clbGeneral.CheckedIndices.Count != 0)
  {
      foreach ( X in clbGeneral.CheckedItems)
      {
          con.Open();
          cmd = new SqlCommand("SELECT serv_id from services where serv_name='" + clbGeneral.SelectedItem.ToString() + "';", con);
          serv_id = (int)cmd.ExecuteScalar();
  
          con.Close();
          transactSQL();
          MessageBox.Show(clbGeneral.SelectedItem.ToString());
      }
  }
  </code></pre>
  
  <p>the transactSQL function:</p>
  
  <pre><code>public void transactSQL() {
      con.Open();
      cmd = new SqlCommand("INSERT INTO transactions VALUES ('" + txtDate.Text + "','" + dataPnts.SelectedRows[0].Cells[3].Value.ToString() + "','" + dataProvs.SelectedRows[0].Cells[4].Value.ToString() + "','" +serv_id.ToString() + "');", con);
      cmd.ExecuteNonQuery();
      con.Close();
  }
  </code></pre>