User clicks a btnAdd and it transfers items to a listBox1. Now I want to create a query that creates a loop from the listBox1 to SELECT FROM a table from SQL and add the result items to listBox2
I have this sample code but it's not working. Can someone help me?
public void add()
{
var con = new DBConnection();
try
{
for (int i = 0; i < listBServices.Items.Count; i++)
{
SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = '" +
listBServices.Items.ToString() + "';", con.Connection);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
int price = rd.GetInt32(0);
listBPrice.Items.Add(price.ToString());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I get this exeption:
