Populate ListView From ComboBox Select
Hello All:
I am trying to perform a sql query once a selection is made in a combobox. I do not have an ok button or anything like that. I would just like to select the item in the drop down and it perform a lookup and populate the listview box. I am able to do a query and to populate the combobox. What the gist of this program is when I select an item in the combobox, it will list all the sets that the item belongs to in the listview box. 'Sets' is the only category in the Listview Box.
I am open for any easier ways to accomplish this. I can do one db lookup (which is probably better) than doing two lookups. I was just trying to use the Select Distinct command to populate the ComboBox with just one item name instead of duplicates.
Thanks all,
Tony
Here is a code snippet:
================================================== ====================
#region Item Lookup
private void ItemLookup()
{
cbItem.Items.Clear();
String connString = XXXLib.XXXLogon.XXXLogObj.GetConnectionString();
SqlConnection conn = new SqlConnection(connString);
conn.Open();
try
{
SqlCommand cmd = new SqlCommand("select distinct cin.name as Item " +
"from catalogname as cin " + where cin.isitem = 0",conn);
SqlDataReader dr = cmd.ExecuteReader();if ( null != dr )
{
while (dr.Read())
{
cbItem.Items.Add(dr["Item"].ToString());
}
dr.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
private void cbItem_DropDown(object sender, System.EventArgs e)
{
cbItem.Items.Clear();
lvSets.Items.Clear();
string sItem = cbItem.SelectedValue.ToString();
string connString = XXXLib.XXXLogon.XXLogObj.GetConnectionString();
SqlConnection conn = new SqlConnection(connString);
conn.Open();
try
{
SqlCommand cmd = new SqlCommand("select distinct ocs.name as Set " +
"from order as o " +
"join catalogname as cin on o.catalogmasteritemguid = cin.masteritemguid and cin.name = "+ sItem + " " +
"join catalogset as ocs on ocs.guid = o.setguid",conn);
SqlDataReader dr = cmd.ExecuteReader();
if ( null != dr )
{
while (dr.Read())
{
ListViewItem lvi = new ListViewItem(dr["Set"].ToString());
lvOrdersets.Items.Add(lvi);
}
dr.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
}
#endregion
|