title: Bind a DropDown in ListView in Edit & Insert Template
body: <p>In my ASP.NET 4.5 WebForms app, In my ListView I am trying to bind DropDownList, but am not able to do it. Actually, the Model that is bind to ListView, has a Property of UnitdirectionId &amp; on Edit &amp; Insert Templates' I want to show the dropdown of all UnitDirection table.</p>
  
  <p>Here is my EditTempalte :</p>
  
  <pre><code>    &lt;EditItemTemplate&gt;
             &lt;tr&gt;
                 &lt;td&gt;
                     &lt;asp:TextBox ID="unitDirTxt" runat="server" Text='&lt;%# Bind("UnitDirectionId") %&gt;'&gt;&lt;/asp:TextBox&gt;
                      &lt;!-- &lt;asp:DynamicControl runat="server" DataField="UnitDirectionId" ID="UnitDirectionId" Mode="Edit" /&gt;  --&gt;
                     &lt;asp:DropDownList ID="unitDirEditDrop" runat="server" ItemType="VincitoreCRMApplication.Models.UnitDirection"
                         DataTextField="DirectionTitle" DataValueField="UnitDirectionId" 
                         SelectMethod="GetUnitDirections"&gt;&lt;/asp:DropDownList&gt;  
                 &lt;/td&gt; 
            &lt;/tr&gt;
  
          &lt;/EditItemTemplate&gt;
  </code></pre>
  
  <p>In Code behind, </p>
  
  <pre><code>    public IQueryable&lt;UnitDirection&gt; GetUnitDirections()
      {
          return _db.UnitDirections;
      }
  </code></pre>
  
  <p>Now, the Model of LsitView has the proerpty as follows :</p>
  
  <pre><code>    [Display(Name= "Unit Direction")]
      public int UnitDirectionId { get; set; }
  
      [ForeignKey("UnitDirectionId")]
      public virtual UnitDirection UnitDirection { get; set; }
  </code></pre>
  
  <p>Now, the UnitDirection Model :</p>
  
  <pre><code>public class UnitDirection
  {
      [ScaffoldColumn(false)]
      public int UnitDirectionId { get; set; }
  
      [Required]
      [Display]
      public string DirectionTitle { get; set; }
  }
  </code></pre>
  
  <p>ERROR that I get is : </p>
  
  <pre><code> ########## ERRO : An error occurred while executing the command definition. See the inner exception for details. 
   STACK :    at   System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteSto reCommands(EntityCommand entityCommand, CommandBehavior behavior)
    at   System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResul    tType](ObjectContext context, ObjectParameterCollection parameterValues)
    at System.Data.Entity.Core.Objects.ObjectQuery`1.&lt;&gt;c__DisplayClass7.&lt;GetResults&gt;b__6()
    at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
     at System.Data.Entity.Core.Objects.ObjectQuery`1.&lt;&gt;c__DisplayClass7.&lt;GetResults&gt;b__5()
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
     at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
    at System.Data.Entity.Core.Objects.ObjectQuery`1.&lt;System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator&gt;b__0()
    at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
    at System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource)
    at System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e)
    at System.Web.UI.WebControls.ListControl.PerformSelect()
    at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
    at System.Web.UI.Control.DataBindChildren()
    at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
    at System.Web.UI.Control.DataBind()
    at System.Web.UI.WebControls.ListView.CreateItemsWithoutGroups(ListViewPagedDataSource dataSource, Boolean dataBinding, InsertItemPosition insertPosition, ArrayList keyArray)
    at System.Web.UI.WebControls.ListView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding)
    at System.Web.UI.WebControls.ListView.PerformDataBinding(IEnumerable data)
    at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)
    at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
     at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
    at System.Web.UI.WebControls.ListView.PerformSelect()
    at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
    at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
    at System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e)
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Control.PreRenderRecursiveInternal()
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  </code></pre>
  
  <p>In db the UnitDirection table has 3 items. </p>
  
  <p>With the  above drop down, I believe I should atleast get list of all UnitDirection in drop down. But on that I am getting error. Then comes to set the selected item in Edit Template.</p>
  
  <p>Can you please help me identify why I am getting this error ?? 
  Any help is highly appreciated.</p>
  
  <p>Thanks</p>