I have combo box of programme and another is courses..
when i select any programme combo box so courses combobox add courses with respect to programme...
the problem is when select another programme so previously programm's course also included in combobox..
i want to remove previously courses..in courses combobox.

private void Teacher_Courses_Load(object sender, EventArgs e)
        {
            MyConnection = new SqlConnection("Data Source=.;Initial Catalog=Bahria_Managment;Integrated Security=True");
            MyDataAdapter = new SqlDataAdapter("select P_Name from tblProgramme", MyConnection);
            MyDataSet = new DataSet();
            MyDataAdapter.Fill(MyDataSet);

            for (int i = 0; i < MyDataSet.Tables[0].Rows.Count; i++)
            {
                    int j = 0;
                    string PName = (string)MyDataSet.Tables[0].Rows[i][j];
                    j++;
                    cmbProgramme.Items.Add(PName);
                
            }

       

          

        }

        private void cmbProgramme_SelectedIndexChanged(object sender, EventArgs e)
        {

            cmbCourses.Items.Clear();
            MyDataAdapter = new SqlDataAdapter("select C_Name from tblCourses where P_Name = '" + cmbProgramme.Text + "'", MyConnection);
            MyDataAdapter.Fill(MyDataSet, "C_Name");

            for (int i = 0; i < MyDataSet.Tables["C_Name"].Rows.Count; i++)
            {
                int j = 0;
                string C_Name = (string)MyDataSet.Tables["C_Name"].Rows[i][j];
                j++;
                cmbCourses.Items.Add(C_Name);
            }
            
        }
    }

just use

private void cmbProgramme_SelectedIndexChanged(object sender, EventArgs e)
        { 
           cmbCourses.Items.Clear();
           cmbCourses.Text="";

           //Your code goes here
        }

Cheers....

cmbCourses.Items.Clear();

method should clear all the items from comboBox. if not try with additional code ( both):

cmbCourses.SelectedIndex = -1;

Not working again...
when i select Programme combobox..so previous values remains in course combobox...
what should i do now?

You mean this code:

private void cmbProgramme_SelectedIndexChanged(object sender, EventArgs e)
{ 
  //DO NOT USE THIS NOW ANY MORE!!
  cmbCourses.Items.Clear();  //you say this is not working??
  // And the rest

This has to work, especially becuase you ADD items to comboBox - your comboBox is not dataBound!!

... but
Lets change the code a bit to:

SqlCommand cmd = new SqlCommand(@"SELECT C_Name FROM tblCourses WHERE P_Name = '" + cmbProgramme.SelectedItem.ToString() + "'", MyConnection);
MyDataAdapter = new SqlDataAdapter(cmd);
//You dont need to fill comboBox item by item, you can set the it`s dataSouce to the dataTable:
cmbCourses.DataSource = table;
cmbCourses.DispayValue = "C_Name";
cmbCourses.ValueMember= cmbCourses.DisplayMember;
//nothing else!

//AND BTW: if you use DataSource property - you have to remove the line of comboBox.Items.Clear() method !

Check it out, and let me know if it works...

cmbCourses.DataSource = table;  //what is table in this sentex?
cmbCourses.DispayValue = "C_Name";
cmbCourses.ValueMember= cmbCourses.DisplayMember;

huhh soree i guys i found My mistake..
i was not initialized dataset object :( it was my mistake..
now my previous code is working well..
thanks 4 support..
Farhad

Hi,
Im glad you found the source of the problem.
About your last question:

cmbCourses.DataSource = table;  //what is table in this sentex?

You are saying that you have a DataSet, which must have at least on DataTable (table is the variable name), so it could be this way:

cmbCourses.DataSource = yourDataSet.Tables["tableName"];  //instead of tableName, you can specify the table index, like [0] - thats the 1st table in the dataSet.

Hi,

Thanks Mutja for support :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.