Click here to Skip to main content
13,705,195 members
Rate this:
 
Please Sign up or sign in to vote.
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 14-Jun-18 9:57am
Updated 19-Jun-18 2:26am
Comments
‭011111100010‬ 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 20 Jun 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100