title: ASP.NET C# - Refresh 2nd DropdownList based on select of 1st dropdownlist
body: <p>I am new to ASP.NET</p>
  
  <p>I have this code Page1.aspx for the DetailsView:</p>
  
  <pre><code>                &lt;InsertItemTemplate&gt;
                      &lt;asp:DropDownList id="VendorName" datasourceid="VendorSqlDataSource"
                          datatextfield="VendorName" DataValueField="VendorID"  
                          SelectedValue='&lt;%# Bind("VendorID") %&gt;' runat="server" AutoPostBack="true" /&gt;
                  &lt;/InsertItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList id="VendorName" datasourceid="VendorSqlDataSource"
                      datatextfield="VendorName"  DataValueField="VendorID"  
                      SelectedValue='&lt;%# Bind("VendorID") %&gt;'   runat="server"  OnSelectedIndexChanged="dllVendor_OnSelectedIndexChanged"  AutoPostBack="true" /&gt;
                  &lt;/EditItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  
          &lt;asp:TemplateField HeaderText="Vendor BU"&gt;
                  &lt;ItemTemplate&gt;
                      &lt;asp:Label ID="VendorBUName" runat="Server" style="text-align:left; width:100%" Text='&lt;%# Eval("VendorBUName")%&gt;' Width="70%"/&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;InsertItemTemplate&gt;
                      &lt;asp:DropDownList id="VendorBUName" datasourceid="VendorBUSqlDataSource"
                          datatextfield="VendorBUName" DataValueField="VendorBUID"  
                          SelectedValue='&lt;%# Bind("VendorBUID") %&gt;' runat="server"/&gt;
                  &lt;/InsertItemTemplate&gt;
                  &lt;EditItemTemplate&gt;
                      &lt;asp:DropDownList id="VendorBUName" datasourceid="VendorBUSqlDataSource"
                      datatextfield="VendorBUName"    DataValueField="VendorBUID"  
                      SelectedValue='&lt;%# Bind("VendorBUID") %&gt;'   runat="server"/&gt;
                  &lt;/EditItemTemplate&gt;
              &lt;/asp:TemplateField&gt;
  </code></pre>
  
  <p>I am trying to make the code behind Page1.aspx.cs to refresh the 2nd Dropdown List in this code behind:</p>
  
  <pre><code>(VendorBUName"), but keeps getting a error "".
      protected void OnSelectedIndexChanged(object VendorID)
      {
          string queryString = "SELECT VendorBUID, VendorBUName From MDF_VendorBU WHERE VendorID = @VendorID";
          System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection("ConnectionString");
          System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(queryString, connection);
          command.Parameters.AddWithValue("@VendorID", VendorID);
          connection.Open();
  
          DataTable dt = new DataTable();
          SqlDataAdapter ad = new SqlDataAdapter(command);
          ad.Fill(dt);
  
          /*
          VendorBUName.Items.Clear();
          if (dt.Rows.Count &gt; 0)
          {
  
              VendorBUName.DataSource = dt;
              VendorBUName.DataTextField = "VendorBUName";
              VendorBUName.DataValueField = "VendorBUID";
              VendorBUName.DataBind();
          }
          */
  
          connection.Close(); 
  
  
      }
  
      //protected void dllVendor_OnSelectedIndexChanged(object sender, System.EventArgs e)
      //{ 
          //OnSelectedIndexChanged(VendorName.SelectedValue.ToString());
      //}
  </code></pre>
  
  <p>I am keep getting the "The name 'VendorBUName' does not exist in the current context", looks like code behind is not recognize the 2nd dropdownlist in the Page1.aspx</p>
  
  <p>Please help.  </p>