0

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.stack.imgur.com/AvkkT.png [2]: https://i.stack.imgur.com/E1R1m.png

Camp Nerd
  • 327
  • 1
  • 11

1 Answers1

0

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();
    }

Get selected value from combo box in C# WPF

Siegfried.V
  • 1,508
  • 1
  • 16
  • 34
  • I looked at that earlier and all it does it throw an error. I have a complete database of info that will be pulled off the PartNumber that will fill in the CalCert and Labels. I have searched everywhere and tried different things and i cant figure it out. With the Binding I am not sure how that would affect the other aspects of the database . I started with binding and because the DB is used in multiple places it was causing an issue with performance within the database. So i am having to seperate the database to its own company. – Camp Nerd Sep 22 '21 at 18:41
  • on my side, this is the "wire to database" that is unknown, but as I understood you fill your ComboBox only with strings. So you could bind only the `SelectedValue`. If this option would fit you I can write you the code for that, it's really not a big thing, and am almost sure it will change nothing for database part. – Siegfried.V Sep 22 '21 at 19:18
  • 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(); } – Camp Nerd Sep 22 '21 at 19:21
  • you may not put code like that in comments, it is just unreadable. Better you edit your original post and add that at the end as "Edit", then say in your comment what it is about. – Siegfried.V Sep 22 '21 at 19:23
  • I don't remember exactly, but yes when you begin you cannot do everything. Just Edit your original post, on the end write "Edit", then after that your code. (wait I do it myself), just tell me what you mean with that code. – Siegfried.V Sep 22 '21 at 19:25
  • I tried to answer my own question and it said i was banned. So i guess i will see what happens if they let me answer or not.. I think its pointless that i cannot answer my own question. However thats how the devs of SO wants to be – Camp Nerd Sep 22 '21 at 19:27
  • Important is that you solved it. I may advise you to just close your question then. – Siegfried.V Sep 22 '21 at 19:30
  • you are not banned. I think it's just because you are new user, so you cannot answer your own question. It avoids that many new members (that don't know how to use stackoverflow) make questions then answer to it themselves. Again I'd advise you to close your question, because it is a duplicate (many other questions are the same here). As minimum an admin will note it as a duplicate, or even users will downvote your question. – Siegfried.V Sep 22 '21 at 19:33