I have a combo box that I have wired up to a database. It works which is good. However I am running into a problem were when I select the item in the Combo Box its delayed.
example: I click on item "A" nothing happens. However, when I click another item lets say "B" then it changes my textblock value to the selected item from the Combo Box i first clicked on. So its delayed by a factor one.
My question is why does it not work on the first click but then works on the second click but delayed by one. I am using WPF C#, with the SelectedChanged
my code is as follows:
ComboBox
private void FillModelComboBox()
{
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT distinct NSTPartNum FROM RLWSTable ", cn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ModelComboBox.Items.Add(dr["NSTPartNum"]);
}
cn.Close();
}
SelectedChanged
private void ModelComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM RLWSTable where NSTPartNum='" + ModelComboBox.Text + "'", cn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string modelnumber = dr["ModelNum"].ToString();
TxtCalModelNumber.Text = modelnumber.ToString();
}
cn.Close();
}
Keep in mind that I call the FillComboBox()
with my Initialized component so it populates the Combo Box. I also call my SQL Connection in the other part of the code, however that all works fine.
[enter image description here][1]
The picture above is the main part of the program then the second picture below is the issue.
The Issue is First Click nothing happens.
[enter image description here][2]
Then The second click everything works fine but its delayed. The way the database is set up is we have our part number and then the customers part number. So the identifying key here is the 15K
I clicked on the 15k
and nothing happens, then when I click on the 2.5k
then it changes to the 15k
. Whatever you click on its delayed by a factor of 1 click.
where am I going wrong at, or am I missing something?
Edit*
this fixed the issue
private void ModelComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string ModelNum = ModelComboBox.SelectedItem.ToString();
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM RLWSTable where NSTPartNum='" + ModelNum + "'", cn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string modelnumber = dr["ModelNum"].ToString();
TxtCalModelNumber.Text = modelnumber.ToString();
}
cn.Close();
}
I added the string ModelNum = ModelComboBox.SelectedItem.ToString();
This was added before the connection is opened [1]: https://i.sstatic.net/AvkkT.png [2]: https://i.sstatic.net/E1R1m.png
The problem is `ModelComboBox.Text is updated after SelectionChanged event. This may solve your problem (duplicate?)
private void ModelComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem typeItem = (ComboBoxItem)cboType.SelectedItem;
string value = typeItem.Content.ToString();
cn.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM RLWSTable where NSTPartNum='" + value + "'", cn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string modelnumber = dr["ModelNum"].ToString();
TxtCalModelNumber.Text = modelnumber.ToString();
}
cn.Close();
}