c# text files to fill combobox and listboxes

right now i am uing mysql to populate my comboboxes and listboxes
the problem with that is it takes forever and they freeze up the program even with there being a locla mysl server on my comp

what i would like to to is populate the combo and list box with text files but i need to populate like for like
first line for fisrt lines of each text file but is that going to slow the prgram down with sooo many text files

another way i can do it is via access file or sqlite file but they need to be searchable and i will need to make the file which is a very large database
cabalsdemonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cabalsdemonAuthor Commented:
string filename = "test.txt";
 
if (File.Exists(filename))
{
    string[] lines = File.ReadAllLines(filename);
    Console.WriteLine(lines[3]);
}
 

Open in new window

is kinda what i was thinking but as the listboxselected is selected i need to search where that line is selected for the other text file
0
Karrtik IyerSoftware ArchitectCommented:
Hi cabalsdemon, How many entries are we talking about in the combo-box or list box? Can you share some sample data that you are trying to load in the list box/combo box?
0
cabalsdemonAuthor Commented:
i am using the mysql connector to get some data and populate the list
but what is happening is it is taing forever and freezing

there was 2 combobox which it will need 2 later but
1 combobox "state"
1 listbox "city"
1 listbox "for a callsign"
and a textbox for what link the web browser needs


void Fill_combobox3()
        {
            string constring = @"server=localhost;userid=user;password=pass";
            
            string Query = "select distinct location from tscuscan.station where state= '" + comboBox2.SelectedItem + "'";
            MySqlConnection conDataBase = new MySqlConnection(constring);
            MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            MySqlDataReader myReader;

            try
            {
                conDataBase.Open();
                myReader = cmdDataBase.ExecuteReader();
                while (myReader.Read())
                {
                    string sName = myReader.GetString("location");
                    comboBox3.Items.Add(sName);
                    comboBox3.SelectedIndex = 0;
                    comboBox3.SelectedIndexChanged += new System.EventHandler(comboBox3_SelectedIndexChanged);
                }


                conDataBase.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Open in new window


for the connection

here is the select
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox3.Items.Clear();
            Fill_combobox3();
        }

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

cabalsdemonAuthor Commented:
if i take the second combobox out and only do states and callsigns it still has little freeze
but i dont know what i am doing wrong

now basically its like that
and that freezing is with about 150 rows
i have pleanty more to put in
is there an easier and fresher way with access or text files
i cant figure it out
0
cabalsdemonAuthor Commented:
anyone help please
0
Robert SchuttSoftware EngineerCommented:
It's not exactly clear to me what the end result should do but for starters it looks like a mistake to me that you're adding an event handler inside  a loop.

I wasn't entirely sure so I tested it and the event handler is indeed called a 100 times if I add 100 items and then change the selection so that could certainly explain the freeze.

So I would say put that after  the loop:
                while (myReader.Read())
                {
                    string sName = myReader.GetString("location");
                    comboBox3.Items.Add(sName);
                }
                if (comboBox3.Items.Count > 0)
                {
                    comboBox3.SelectedIndex = 0;
                    comboBox3.SelectedIndexChanged += new System.EventHandler(comboBox3_SelectedIndexChanged);
                }

Open in new window

Also might wanna change the combo2 handler to match:
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) {
            if (comboBox3.Items.Count > 0) {
                comboBox3.SelectedIndexChanged -= new System.EventHandler(comboBox3_SelectedIndexChanged);
                comboBox3.Items.Clear();
            }
            Fill_combobox3();
        }

Open in new window

Of course, if it's ok to have the handler active all the time (just check that a valid selection is made or disable empty list/comboboxes) then it's much easier to create it only once, for example in your Form_Load() or even using the designer; then there's no need to keep removing and adding it. I did it this way now to keep the working of your code the same, since that only creates the handler if there is data returned from the query.

Here's a picture to show how you can add an event handler using the designer, just double-click where I've typed an "x".
capture
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cabalsdemonAuthor Commented:
thank you much for that it works now
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.