Search code examples
c#mysqlcomboboxnotnull

Get rid of all null values in Combobox after iport data from mysql database


I'm developing some winforms application for my office. in my application I have more than 50 comboboxes that I want to connect the MySQL database and retrieve data. every combobox should retrieve data from certain MySQL database column. any way I successfully made a connection and retrieve data to comboboxes using below code.

but now I have very strange problem. in my MySQL database there are lot of null values (some columns contain 30 rows while some only 1 or 2). after data retrieves the comboboxes the first combobox (which related to second column) display data correctly but start from 2nd column it shows only one or two rows. all the other values in column not showing. I went through some forums and set my al null values to empty string. but after that comboboxes shows me list of blank entries after last field.

I also tried IS NOT NULL after my query, that also not working :(

how to overcome this situation ? I want to retrieve data to all the comboboxes and without any empty or null values. I'm using .net framework 2.0

is this possible to programmatically in C# or SQL query, or should I change my database structure ?

(I don't have good knowledge in MySQL. I use workbench for create database :( )

This is my current code

 void combobox()
    {

        string constring = string.Format("datasource='{0}';port='{1}';database='{2}';username=****************;password=************;Connect Timeout=180;Command Timeout=180", dbserverip.Text,curport.Text,currentdb.Text);
        string Query = "select * from estifdb.customconditions ;";
        MySqlConnection conn = new MySqlConnection(constring);
        MySqlCommand cmd = new MySqlCommand(Query, conn);
        MySqlDataReader myreader666;

        try
        {
            conn.Open();
            myreader666 = cmd.ExecuteReader();


            while (myreader666.Read())
            {

                string sName = myreader666.GetString("someval1");
                applicationcombobox.Items.Add(sName);

                string sName2 = myreader666.GetString("someval2");
                applicationcombobox2.Items.Add(sName2);

                string sName3 = myreader666.GetString("someval3");
                applicationcombobox3.Items.Add(sName3);

                string sName4 = myreader666.GetString("someval4");
                applicationcombobox4.Items.Add(sName4);

                string sName5 = myreader666.GetString("someval5");
                applicationcombobox5.Items.Add(sName5);


               // and 50 left

Solution

  • Have you also tried something like this:

    string sName = myreader666.GetString("1003");
    if(sName != null && !sName.Equals(""))
        applicationcombobox.Items.Add(sName);