Hi I'm pritty new to C# I'm having trouble with items repeating in a combo box
This method gets the values from my table and stores it in the list of strings which it does fine . . . I think.
public class EditStates {
public List<string> getStates(CEditStates ced)
{
string typeSelected = (string)ced.cb1.SelectedItem;
string states = "SELECT State FROM MyTables.StateDefaults
WHERE ObjectTypeID = '" + typeSelected + "'";
SqlCommand cmd = new SqlCommand(states, db.Connect());
SqlDataReader reader;
List<string> objStates = new List<string>();
try
{
reader = cmd.ExecuteReader();
while (reader.Read())
{
objStates.Add(reader["State"].ToString());
}
reader.Close();
}
catch
{
MessageBox.Show("Failed to retreive object states");
}
return objStates;
}
}
And this method changes the value of cbo2 and populates cbo3 (which has the repeated values) when cbo1 is changed.
public class CEditeStates {
private void cbo1_SelectedIndexChanged(object sender, EventArgs e)
{
string newobjDesc = ed.getNewDesc(this);
getObjectTypeState() method
List<string> _objState = ed.getStates(this);
if (cbo2.SelectedItem != null && cbo3.Items == null)
{
if (cbo2.SelectedItem.ToString() != newobjDesc)
{
cboB2.SelectedItem = newobjDesc;
foreach (string str in _objState)
{
cbo3.Items.Add(str);
}
}
}
}
}
Can anyone see why cbo3 repeats the values ?
Thanks in advance