Search code examples
c#sqliteselectcomboboxdatareader

C# SQLite Parameters in SELECT statement - Dependent Comboboxes issue


I've been having some problems executing this query in SQLite in C# - Visual Studio 2008. I have 2 comboboxes, the second one depends on the value selected in the first one (i.e: Province --> valid City for that province). I've searched the forums but I haven't figured out why this particular query returns 0 results. Am I setting the parameters correctly? Any suggestions will be greatly appreciated since I'm new at this. Thanks!

string provName = this.comboProvDest.GetItemText(this.comboDestProv.SelectedItem);
string queryDestCity = "SELECT d_city FROM Cities WHERE id_prov = @provName ";

SQLiteCommand cmCity = new SQLiteCommand(queryDestCity, conn);
cmCity.Parameters.AddWithValue("@provName", provName );
SQLiteDataReader drCity = cmCity.ExecuteReader();

 comboDestProv.Items.Add("");

 while (drCity.Read())
 {
      comboDestCity.Items.Add(drCity["d_city"].ToString());
      this.comboDestCity.DropDownStyle = ComboBoxStyle.DropDownList;
 }

Solution

    1. Put "this.comboDestCity.DropDownStyle = ComboBoxStyle.DropDownList;" after the "while..." loop
    2. Add "this.comboDestCity.Items.Clear()" before the loop
    3. Add "this.comboDestCity.Refresh" after the loop