DropDownList continued

Last post 11-26-2007 8:37 AM by robert.westerlund. 1 replies.

Sort Posts:

  • DropDownList continued

    11-26-2007, 8:09 AM
    • Loading...
    • mangol
    • Joined on 11-20-2007, 1:27 PM
    • Posts 3

    Hi EveryOne

    Here is the newer version of the problem i discussed and was solved with all your help.

    I now have 3 DropDownLists. Now the idea is the same I populated the first DropDownList on PageLoad. Then OnSelectedIndexChanged i popolated the second one. Then on selection on the second one i populated the third one.

    [PROBLEM] The problem is the same that the second DropDownList is now doubling and trippling the seleted items. I mean it concatenates the new items with the old ones, even the searching criteria is changed. I tries DropDownList7.Items.Clear(), but it is not working.

    Here is the sample code i m trying to run.


    void Page_Load(Object s,EventArgs e)
    {
      objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("/") + "aeg.mdb");
      objConn.Open();
      objCmd = new OleDbCommand("SELECT DISTINCT Country from Institutes",objConn);
      objReader = objCmd.ExecuteReader();

    if(! IsPostBack)
      while(objReader.Read())
       {
       DropDownList6.Items.Add(Convert.ToString(objReader["Country"]));
       }
    }

    Then on Selected Index change:

    void DropDownList6_SelectedIndexChanged(object sender, EventArgs e) {

     objCmd = new OleDbCommand("SELECT DISTINCT City from Institutes where Country = '"+ DropDownList6.SelectedItem.Text + "'",objConn);
    objReader = objCmd.ExecuteReader();

      while(objReader.Read())
       {
      DropDownList7.Items.Add(Convert.ToString(objReader["City"]));

        }

    }

              void DropDownList7_SelectedIndexChanged(object sender, EventArgs e) {
        
                   objCmd = new OleDbCommand("SELECT DISTINCT Institute from Institutes where Country = '"+ DropDownList6.SelectedItem.Text + "' and City = '" + DropDownList7.SelectedItem.Text + "'",objConn);
                   objReader = objCmd.ExecuteReader();
        
                   while(objReader.Read())
                   {
                     DropDownList8.Items.Add(Convert.ToString(objReader["Institute"]));
        
                   }
        
             }

  • Re: DropDownList continued

    11-26-2007, 8:37 AM
    Answer
    The solution to your problem should be exactly the one you proposed yourself. Make sure you remove all items from the DropDownList before you add new items.

            void DropDownList6_SelectedIndexChanged(object sender, EventArgs e)
            {
    
                objCmd = new OleDbCommand("SELECT DISTINCT City from Institutes where Country = '" + DropDownList6.SelectedItem.Text + "'", objConn);
                objReader = objCmd.ExecuteReader();
    
                DropDownList7.Items.Clear();
                while (objReader.Read())
                {
                    DropDownList7.Items.Add(Convert.ToString(objReader["City"]));
    
                }
    
            }
    
            void DropDownList7_SelectedIndexChanged(object sender, EventArgs e)
            {
    
                objCmd = new OleDbCommand("SELECT DISTINCT Institute from Institutes where Country = '" + DropDownList6.SelectedItem.Text + "' and City = '" + DropDownList7.SelectedItem.Text + "'", objConn);
                objReader = objCmd.ExecuteReader();
    
                DropDownList8.Items.Clear();
                while (objReader.Read())
                {
                    DropDownList8.Items.Add(Convert.ToString(objReader["Institute"]));
    
                }
    
            }

     

     

    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter