I am trying to put the results of an SQL statement into a combo box on a form. I have trawled the net for an answer and I can’t get further than this:
using (SqlCeConnection c = new SqlCeConnection(
Properties.Settings.Default.CW2_DBConnectionString))
{
c.Open();
List<Visit> rows = new List<Visit>();
SqlCeCommand cmd = c.CreateCommand();
cmd.CommandText = "SELECT PICKUP_ID FROM PICKUP";
c.Open();
cmd.ExecuteNonQuery();
SqlCeDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
}
cmbPickupID.Items.Add(rows);
}
I am not sure what to put in the while loop. I did have something like:
rows.Add(reader["PICKUP_ID"].ToString());
But obviously it didn’t work. The Visit class has set and get for VisitID. Does anyone have any ideas?
First off: You don’t need the
cmd.ExecuteNonQuery();Secondly: if your
Visitclass has a property PICKUP_ID, and you are using a List ofVisititems, then you need to add actualVisitsto your list, not just strings, like this:if you only need the id’s, then you might as well just make a
List<string>. The code you posted would work then.