简体   繁体   中英

How to display items in CheckListBox from ComboBox

I have a comboBox and a checkListBox in my windows form application that connected to my SQL database. I got the binding data part working, but I am not sure how to show the datas in checkListBox when the comboBox item is selected. Let say I have 10 items in my comboBox that bind with my SQL database and they are under the column name ("application name ") such as excel, word, android, eclipse etc.... I call this method when the form begin to load. Sorry for the long code.

Here is my code for that applicationComboBox

private void loadComboBox()
        {
            myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");

            try
            {
                myConn.Open();

                //my table name is Application_Detail
                string query = "select * from Application_Detail";

                myCommand = new SqlCommand(query, myConn);

                //reading the value from the query
                SqlDataReader dr = myCommand.ExecuteReader();

                //Reading all the value one by one
                while (dr.Read())
                {
                    //column is 1 in Application_Detail Data
                   //GetString(1) display the 2nd column of the table
                    string name = dr.GetString(1);

                    //display the application name in column 2 - 
                    applicationComboBox.Items.Add(name);
                }
                myConn.Close();
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

The outcome of this part of code is:

 //label Name        //Application Name
  Application Name:                    
                      Excel
                      Word
                      NotePad
                      PowerPoint
                      SubLime
                      Eclipse

After I call this method, I want to display the teacher name that is according to what the user selected in this applicationComboBox . So if teacher 1,2,3 is using Excel and the user selected excel from the comboBox, the checkListBox will display teacher 1,2,3 and vice versa. To do this, I call the method at the comboBox1_SelectedIndexChanged method because I want to display the detail when I select an item from the comboBox. Below is my code

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{

    try
    {
          //I check if the comboBox index is at 0, it disable the button. 
            if (applicationComboBox.SelectedIndex == 0)
            {
                exportButton.Enabled = false;
                this.teacherCheckListBox.DataSource = null;
                teacherCheckListBox.Items.Clear();
            }
          //it it is not at 0, 
            else
            {

                exportButton.Enabled = true;
                 //call this method
                fill_checkListBox();

            }
            //teacherCheckListBox

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

    private void fill_checkListBox()
        {
            myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");

            try
            {
                myConn.Open();

                //for reading purpose, I break down by long statement 
                //In this statement, I left join 3 table (Teacher_Detail,  AppUser_Detail, and Application_Detail table). My AppUser_Detail contains all 3 id (teacherId, applicationId, and AppUserId). I then set filter the table using `where` keyWord to make the applicationId = the comboBox text 

                string query = "SELECT
                                  td.chineseName,
                                  ad.applicationId,
                                  aud.applicationId,
                                  ad.applicationName 
                              FROM[AppUser_Detail] as aud 
                              LEFT JOIN[Teacher_Detail] as td 
                              ON aud.teacherId = td.teacherId

                              LEFT JOIN[Application_Detail] as ad 
                              ON aud.applicationId = ad.applicationId

                              where aud.applicationId = '" + applicationComboBox.Text + "' AND NOT(td.teacherId IS NULL)
";
                myCommand = new SqlCommand(query, myConn);


                //reading the value from the query
                SqlDataReader dr = myCommand.ExecuteReader();

                //Reading all the value one by one
                while (dr.Read())
                {
                    //column is 0 where the teacherName belong in my Teacher_Detail table

                    string name = dr.GetString(0);

                     //I tried to set the text of the checkListBox as the teacherName, but I can't somehow
                    teacherCheckListBox.Text = name;

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

When I run the program like this, it said Conversion failed when converting the varchar value "Excel" to data type int. Is there a way to fix it? it shouldn't be a problem because in my Application_Detail table, my applicationName's and my teacherName's data type is set as nvarchar(50) and applicationId and teacherId = int;

The problem is with this line, I would think:

 where aud.applicationId = '" + applicationComboBox.Text +

Based on your code, I would think that applicationId is an int and applicationComboBox.Text is just that, text.

Try this:

where ad.applicationName = '" + applicationComboBox.Text.Trim() +

Try this:

if (string.IsNullOrWhiteSpace(teacherCheckListBox.FindString(name))
{
    teacherCheckListBox.Items.Add(name);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM