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 foreach statement)
Here's my code: (clbGeneral is the name of the checklistbox)
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());
}
}
the transactSQL function:
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();
}