title: C# has a SelectedIndex which is invalid because it does not exist in the list of items
body: <p>After selecting the continents, country, state its working fine. When I select again select continent it is throwing an error. I have created <code>tblcontinents</code>, <code>tblcountries</code>, <code>tblstates</code>, I have kept foreign key relation between <code>tblcontient</code> and <code>tblcountry</code>, <code>tblcountry</code> and <code>states</code>. and created the stored procedure. </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Data;
  
  namespace WebApplication3
  {
      public partial class WebForm1 : System.Web.UI.Page
      {
  
          protected void Page_Load(object sender, EventArgs e)
          {
              if (!IsPostBack)
              {
                 DataSet ds= getname("spgetcontinentbyid", null);
                 dropdownlist1.DataTextField = "ContinentName";
                 dropdownlist1.DataValueField = "id";
                 dropdownlist1.DataSource = ds;
                 dropdownlist1.DataBind();
  
                 ListItem li1 = new ListItem("Select Continent", "-1");
  
                 dropdownlist1.Items.Insert(0,li1);
  
                 ListItem li2 = new ListItem("Select Country", "-1");
  
                 dropdownlist2.Items.Insert(0, li2);
  
                 ListItem li3 = new ListItem("Select State", "-1");
  
                 dropdownlist3.Items.Insert(0, li3);
  
                 dropdownlist2.Enabled = false;
                 dropdownlist3.Enabled = false;
  
  
  
  
              }
          }
  
          private DataSet getname(string spname, SqlParameter sqlparameter)
          {
              string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
              SqlConnection cn = new SqlConnection(cs);
              SqlDataAdapter da = new SqlDataAdapter(spname, cn);
              da.SelectCommand.CommandType = CommandType.StoredProcedure;
              if (sqlparameter != null)
              {
  
                  da.SelectCommand.Parameters.Add(sqlparameter);
  
  
              }
  
  
              DataSet ds = new DataSet();
              da.Fill(ds);
              return ds;
  
  
  
  
          }
  
          protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
          {
              if (dropdownlist1.SelectedIndex == 0)
              {
  
                  dropdownlist2.SelectedIndex = 0;
                  dropdownlist2.Enabled = false;
  
                  dropdownlist3.SelectedIndex = 0;
                  dropdownlist3.Enabled = false;
  
              }
              dropdownlist2.Enabled = true;
  
              SqlParameter param1 = new SqlParameter("@contintentid", dropdownlist1.SelectedValue);
  
  
               dropdownlist2.DataSource=getname("spgetcountrybyid",param1);
               //dropdownlist2.DataTextField = "CountryName";
               //dropdownlist2.DataValueField = "id";
  
              dropdownlist2.DataBind();
  
              ListItem li = new ListItem("select Country", "-1");
              dropdownlist2.Items.Insert(0,li);
  
              //dropdownlist2.SelectedIndex = 0;
              //dropdownlist2.Enabled = false;
  
              dropdownlist3.SelectedIndex = 0;
              dropdownlist3.Enabled = false;
          }
  
          protected void dropdownlist2_SelectedIndexChanged(object sender, EventArgs e)
          {
              if (dropdownlist2.SelectedIndex == 0)
              {
                  dropdownlist3.SelectedIndex = 0;
                  dropdownlist3.Enabled = false;
              }
              dropdownlist3.Enabled = true;
              SqlParameter param2 = new SqlParameter("@countryid", dropdownlist2.SelectedValue);
              DataSet ds = getname("spgetstatesbycountryid", param2);
              dropdownlist3.DataTextField = "Statename";
              dropdownlist3.DataValueField = "id";
              dropdownlist3.DataSource = ds;
              dropdownlist3.DataBind();
  
              ListItem li = new ListItem("select City", "-1");
              dropdownlist3.Items.Insert(0, li);
  
          }
  
      }
  }
  </code></pre>