ASP.NET DetailsVew rebind the dropdownlist after insert items
<p>I am new to ASP.NET and have been trying to make the dropdownlist works correctly in DetailsView, but no luck so far.  I am getting every close to accomplish this, so much appreciated of someone can help me out here.</p>

<p>An error that I am getting is:</p>

<blockquote>
  <p>Databinding methods such as Eval(), XPath(), and Bind() can only be
  used in the context of a databound control.</p>
</blockquote>

<p>I have this code in my DetailsView:</p>

<pre><code>&lt;EditItemTemplate&gt;  
    &lt;asp:DropDownList id="VendorName" 
        datasourceid="VendorSqlDataSource" 
        AutoPostBack="true" 
        datatextfield="VendorName" 
        DataValueField="VendorID"    
        SelectedValue='&lt;%# Bind("VendorID") %&gt;'   
        runat="server" 
        OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" /&gt; 

    &lt;asp:SqlDataSource ID="VendorSqlDataSource" 
        ConnectionString="&lt;%$Connectionstrings:ConnectionString%&gt;"  
        SelectCommand="SELECT VendorID, VendorName from MDF_Vendor" 
        runat="server"&gt;  
    &lt;/asp:SqlDataSource&gt;  
&lt;/EditItemTemplate&gt;  


&lt;EditItemTemplate&gt; 
    &lt;asp:DropDownList id="VendorBUName" 
        datasourceid="VendorBUSqlDataSource"  
        datatextfield="VendorBUName" 
        DataValueField="VendorBUID"    
        SelectedValue='&lt;%# Bind("VendorBUID") %&gt;'   
        runat="server"/&gt; 

    &lt;asp:SqlDataSource ID="VendorBUSqlDataSource" 
        runat="server" 
        ConnectionString="&lt;%$Connectionstrings:ConnectionString%&gt;" 
        selectcommand="SELECT VendorBUID, VendorBUName 
                       from MDF_VendorBU 
                       Where VendorID = @VendorID" 
        &lt;SelectParameters&gt;  
            &lt;asp:Parameter Name="VendorID" Type="Int32" /&gt;  
        &lt;/SelectParameters&gt;  
    &lt;/asp:SqlDataSource&gt;  
&lt;/EditItemTemplate&gt; 
</code></pre>

<p>In my code behind list, I have this code to rebuild the list of VendorBUName dropdownlist when VendorName selected.  </p>

<pre><code>protected void ddlVendor_SelectedIndexChanged
               (object sender, EventArgs e)
{

    DropDownList ddlVendor =
        (DropDownList)DetailsView1.FindControl("VendorName");
    DropDownList ddlVendorBU =
        (DropDownList)DetailsView1.FindControl("VendorBUName");


    if (ddlVendorBU != null)
    {
        Response.Write("SelectChanged");
        int VendorID = Convert.ToInt32(ddlVendor.SelectedValue.ToString());
        Response.Write("VendorID: " + VendorID);

        ddlVendorBU.Items.Clear();

        string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConn);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "SELECT VendorBUID, VendorBUName FROM dbo.MDF_VendorBU WHERE VendorID = @VendorID";
        cmd.Parameters.AddWithValue("@VendorID", VendorID);
        DataSet objDs = new DataSet();
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        con.Open();
        dAdapter.Fill(objDs);

        con.Close();


        if (objDs.Tables[0].Rows.Count &gt; 0)
        {
            foreach (DataRow dr in objDs.Tables[0].Rows)
            {
                ddlVendorBU.Items.Insert(0, new ListItem( (dr["VendorBUName"]).ToString(), "-1") );
            }
            ddlVendorBU.DataBind();  //&lt;-- Problem HERE can not rebind it.
        }

    }
}
</code></pre>

<p>This is working so far in DetailsView.  I click Edit, these 2 dropdownlists selected the correct value.  When I select another Vendor in VendorName ddl, it refreshes the VendorBUName list.  Great! However, when I try to ddlVendorBU.DataBind(); to rebind the VendorBUName list, it gives the error above.  If removed that, it won't update the record for VendorBUName because is not bound to the field. </p>

<p>I am totally stuck, if someone can please help.  Thanks in advance!</p>
