Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project in winforms and in my RegisterStudent form I have a combobox that hold education column.Based on the selection from the combobox,a selection of checkboxes will appear.The problem is that when i want to select a value from the combobox,i can't.Instead the courses for the default value appears.This is my method to load data into the combobox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    LoadEducation();
}
private void LoadEducation()
{
    using (SqlConnection conn = new SqlConnection(connstr))
    {
        try
        {
            string query = "select  education from AvailableCourses";

            da = new SqlDataAdapter(query, conn);
            conn.Open();
            ds = new DataSet();
            da.Fill(ds, "AvailableCourses");
            comboBoxEducation.DisplayMember = "education";
            comboBoxEducation.ValueMember = "education";
            comboBoxEducation.DataSource = ds.Tables["AvailableCourses"];
        }
        catch (Exception ex)
        {
            // write exception info to log or anything else
            MessageBox.Show("Error occured!");
        }
    }
}

And this is the table from where i take the data:
CREATE TABLE [dbo].[AvailableCourses](
	[CourseID] [char](10) NOT NULL,
	[ClassID] [nvarchar](50) NULL,
	[courseName] [nvarchar](50) NOT NULL,
	[education] [nvarchar](50) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[CourseID] ASC


What I have tried:

Does anybody have any idea why is not working?I have also tried with SelectedIndex and SelectedValue but for nothing,it won't do anything.Thank you in advance.
Posted
Updated 19-Jun-18 2:26am
Comments
ZurdoDev 14-Jun-18 16:45pm    
".The problem is that when i want to select a value from the combobox,i can't" - What does this mean? The comboBox is disabled?

"Instead the courses for the default value appears." What does this mean?

Your code is showing the education field from the AvailableCourses table.

It is not clear at all how we can help you.
Eliza Maria 14-Jun-18 16:51pm    
I can't change the value from the combobox.If it starts on the default value "Item1" when i want to change to "item2" it won't work at all.
Eliza Maria 14-Jun-18 16:55pm    
And I also declare the function in Form_Load,but it only loads the values into the combobox.

You don't use the combobox selection in that code to decide which row of your table to access - and the columns you access as a DisplayMember and ValueMember are not in your database table!
Probably, you need
comboBoxEducation.DisplayMember = "courseName";
comboBoxEducation.ValueMember = "CourseID";

And then to use the SelectedIndexChanged event of comboBoxEducation to update your checkboxes.
 
Share this answer
 
Comments
Eliza Maria 15-Jun-18 4:49am    
Thank you for your response.Please look carefully because if you can see,it is in the table:[education] [nvarchar](50) NOT NULL and i need only that,not coursename nor courseid.These are totally different things.
I have manage to find the problem.I had a method that would take all the courses specific to the value of the combobox and,first of all I deleted the LoadEducation() from the SelectedIndexChanged for combobox and added the method which takes the courses spcific to that education.
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          this.PopulateCheckBoxes();

      }

After,I deleted it(
PopulateCheckBoxes
) from Form_Load because it would have selected the specific courses on top of the ones already there and would have not updated after(that was also one of my problems).I only had the method LoadEducation() in Form_Load() and I deleted it from
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
.Now it works perfectly for me.Thank you everyone for your help and I hope this could be useful for young developers with the same problem.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900